[R] ?nchar ?strsplit

David L Carlson dcarlson at tamu.edu
Mon Aug 27 22:07:09 CEST 2012


Splitting is easy:

strsplit(as.character(x$ID), "/")

That produces a list with four elements, each of which is a character
vector. R does not have a representation for "sub-columns" so you will have
to be clearer about how you want to represent the data and what you are
planning to do with it.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Sapana Lohani
> Sent: Monday, August 27, 2012 1:41 PM
> To: R help
> Subject: [R] ?nchar ?strsplit
> 
> 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.
> 
> Thanks
> 
> 	[[alternative HTML version deleted]]




More information about the R-help mailing list