On Wed, Aug 4, 2010 at 3:40 PM, Dimitri Liakhovitski <dimitri.liakhovit...@gmail.com> wrote: > Thanks a lot, David. > It works perfectly. Of course, lapply is also a loop! > > So, your method is: > z<-data.frame(nam1=c("bbb..aba","ccc..abb","ddd..abc","eee..abd"),stringsAsFactors=FALSE) > z$nam2<-unlist(lapply( strsplit(z[[1]],split="\\.."), "[", 1)) > z$nam3<-unlist(lapply( strsplit(z[[1]],split="\\.."), "[", 2)) > > And using the new package "stringr" (thank you for sharing!): > y<-data.frame(nam1=c("aaa..aba","bbb..abb","ccc..abc","ddd..abd"), > stringsAsFactors=FALSE) > library(stringr) > y$nam2<-as.data.frame(str_split_fixed(y$nam1, "\\..", 2))[[1]] > y$nam3<-as.data.frame(str_split_fixed(y$nam1, "\\..", 2))[[2]] > (y) >
Using sub would be a bit easier than strsplit and its alternatives: > x <- > data.frame(nam1=c("bbb..aba","ccc..abb","ddd..abc","eee..abd"),stringsAsFactors=FALSE) > sub("\\..*", "", x$nam1) # first part [1] "bbb" "ccc" "ddd" "eee" > sub(".*\\.", "", x$nam1) # last part [1] "aba" "abb" "abc" "abd" ______________________________________________ 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.