[R] applying strsplit to a whole column

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 4 21:52:12 CEST 2010


On Wed, Aug 4, 2010 at 3:40 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at 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"



More information about the R-help mailing list