开发者

How to import unusual data formats into R?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜