[R] ?nchar ?strsplit

David Winsemius dwinsemius at comcast.net
Tue Aug 28 05:01:37 CEST 2012


On Aug 27, 2012, at 1:40 PM, Sapana Lohani wrote:

> Hi, my data frame is
>
> x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))
>
> I want to split my column ID using "/" as the place to split. How  
> can I do that without telling the code how many sub-columns. I could  
> use nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row  
> of the column, but could not use it to split ID in.

 > read.table(text=as.character(x$ID), sep="/", fill=TRUE, as.is=TRUE)
    V1  V2  V3 V4
1 abc def
2 abc def ghi
3 abc
4 mno pqr  st ab

-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list