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]]
(Re-)read ?strsplit. You do not have to tell the code how many columns ... etc. And the split isn't into sub columns. y <- as.character(x[[1]] ## You need a character vector argument strsplit(y,"/") ## works -- Bert On Mon, Aug 27, 2012 at 11:40 AM, Sapana Lohani <lohani.sapana at ymail.com> 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. > > Thanks > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
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]]
HI, #In addition to, nchar1<-nchar(gsub("[^/]","",x$ID)) ?max(nchar1) #[1] 3 library(stringr) ?data.frame(str_split_fixed(x$ID,"/",max(nchar1)+1)) #or strsplit(as.character(x$ID),"/") ? #You can also use: strsplit(gsub("(.*)/(.*)","\\1 \\2",gsub("(.*)/(.*)/(.*)","\\1 \\2 \\3",gsub("(.*)/(.*)/(.*)/(.*)","\\1 \\2 \\3 \\4",x$ID)))," ") #[[1]] #[1] "abc" "def" #[[2]] #[1] "abc" "def" "ghi" #[[3]] #[1] "abc" #[[4]] #[1] "mno" "pqr" "st"? "ab" A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: R help <r-help at r-project.org> Cc: Sent: Monday, August 27, 2012 2:40 PM 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]] ______________________________________________ R-help at 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.
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