[R] Import variable labels to data frame columns

Jack Tanner ihok at hotmail.com
Mon Mar 28 05:49:41 CEST 2011


AjayT <ajaytalati <at> googlemail.com> writes:

> The data looks like this,
> 
> Id=1 time=2011-03-27 19:23:40 start=1.4018       end=1.4017   
> Id=2 time=2011-03-27 19:23:40 start=1.8046       end=1.8047 

Something like this would do:

lines = scan(file, nlines=1, ...)
fields = strsplit(lines[1], "\s+", perl=TRUE)
k.v.pairs = sapply(fields, function(f) {
  strsplit(f, "=")
})
df.row = sapply(k.v.pairs, function(k.v) {
  k.v[2]
})

You can then rbind() the df.row values to get a data.frame. Note that this
assumes that all your input records have all the same fields and all in the same
order.



More information about the R-help mailing list