On May 3, 2012, at 14:05 , Nutter, Benjamin wrote: > So long as x is a character vector, I tend to use the following for this > problem. > >> x <- c("12/31/11 23:45", "1/1/12 2:15") >> >> x.split <- strsplit(x, " ") >> >> x.date <- sapply(x.split, function(y) return(y[1])) >> x.time <- sapply(x.split, function(y) if (length(y) > 1) return(y[2]) else >> NA) >> >> x.date > [1] "12/31/11" "1/1/12" >> x.time > [1] "23:45" "2:15" >> > > Benjamin Nutter | Biostatistician | Quantitative Health Sciences > Cleveland Clinic | 9500 Euclid Ave. | Cleveland, OH 44195 | (216) > 445-1365 >
I think this can be simplified to > sapply(x.split,`[`,1) [1] "12/31/11" "1/1/12" > sapply(x.split,`[`,2) [1] "23:45" "2:15" It's a bit inefficient, though. Other ideas: > sub(" .*$", "", x) [1] "12/31/11" "1/1/12" > sub("^.* ", "", x) [1] "23:45" "2:15" > read.table(text=x) V1 V2 1 12/31/11 23:45 2 1/1/12 2:15 -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd....@cbs.dk Priv: pda...@gmail.com ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.