开发者

How to import unusual data formats into R?

开发者 https://www.devze.com 2023-01-05 18:54 出处:网络
Format consist of lines, every line has set of key=\"value\" elements. Format example: X=\"1\" Y=\"2\" Z=\"who are you?\"

Format consist of lines, every line has set of key="value" elements.

Format example:

X="1" Y="2" Z="who are you?"
Y="4" Z="bla bla..."
X="42"

I would like to import this data into R, table or data.frame, whe开发者_运维知识库re key defines column.


The following code parses the file you provided in a 'melted' form:

data<-NULL 
stream<-file("path");open(stream) #or stream<- textConnection(' X="1" Y="2" Z="who are you?" Y="4" Z="bla bla..." X="42"')
while(length(ele<-c(scan(stream,what="string",n=1,sep="="),scan(stream,what="string",n=1,sep=" ")))>0){
    data<-rbind(data,ele);
}
close(stream);
print(data);

Now crystallizing:

 sapply(unique(data[,1]),function(key) data[data[,1]==key,2])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号