An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090526/9a63c06c/attachment-0001.pl>
Converting a list to a data frame or columns at the least
2 messages · Farrel Buchinsky, Glen A Sargeant
Farrel Buchinsky-3 wrote:
I have a column in which dates and times are specified thus m/d/yyyy HH:MM:SS Alas, some entries do not include the time and therefore are only m/d/yyyy so I used read.csv and specified that the relevant column should be read as is and it remained as a character variable. I then split the value on the space split.dt.time <-strsplit(teacher$Date.and.Time.of.Lesson," ") that gives me a list where each item on the list has two elements if the time was specified and only 1 element if the time was not specified. How do I take that list and make all the 1st elements go into one column and all the second elements go into a second column; where there is no time I would like the value to be missing (NA) I tried playing around with do.call(rbind... so I tried the following unsuccessfully do.call(rbind,lapply(teacher$Date.and.Time.of.Lesson, function(i) strsplit(i," ")) ) rbind(strsplit (teacher$Date.and.Time.of.Lesson," ")) do.call(rbind(data.frame(strsplit (teacher$Date.and.Time.of.Lesson," "))))
Farrel, You cannot create a matrix or data frame by combining row vectors that are not of the same length. Replace the missing values before calling rbind() and things should work fine.
x.
[[1]] [1] "(01/02/70" "00:00:00)" [[2]] [1] "(01/03/70" [[3]] [1] "(01/04/70" "08:00:00)"
x <- lapply(x.,function(v){
+ if(length(v)<2)v[2] <- NA + v})
do.call(rbind,x)
[,1] [,2] [1,] "(01/02/70" "00:00:00)" [2,] "(01/03/70" NA [3,] "(01/04/70" "08:00:00)"
You probably will wish to clean up dangling parentheses and the like, but I'll leave that to you. Glen
View this message in context: http://www.nabble.com/Converting-a-list-to-a-data-frame-or-columns-at-the-least-tp23720675p23723788.html Sent from the R help mailing list archive at Nabble.com.