I have a data frame like this xx<-data.frame(cbind(Sample=c('Ctrl_6h','1+0_6h','1+200_6h','1+5k_6h','Ctrl_5K_6h','ConA_6h'), IFIT1=c(24,25,24.7,24.5,24.2,24.8))) grep('[[:digit:]]h',xx$Sample) yy<-xx$Sample strsplit(yy,"_") I have to extract the time information separated by '_' in the sample names, i tried grep and strsplit, it looks that i am not providing some information correctly. I would appreciate if someone can point me to the correct way. Thanks Sharad -- View this message in context: http://r.789695.n4.nabble.com/splitting-sample-names-tp3753712p3753712.html Sent from the R help mailing list archive at Nabble.com.
On Aug 18, 2011, at 5:43 PM, 1Rnwb wrote:> I have a data frame like this > xx<- > data > .frame > (cbind > (Sample > =c('Ctrl_6h','1+0_6h','1+200_6h','1+5k_6h','Ctrl_5K_6h','ConA_6h'), > IFIT1=c(24,25,24.7,24.5,24.2,24.8))) > > grep('[[:digit:]]h',xx$Sample) > > yy<-xx$Sample > > strsplit(yy,"_") >You are getting tangled up because of stringsAsFactor + TRUE by default. (The cbind is not needed and may have been confusing things as well.) xx <- data .frame (Sample =c('Ctrl_6h','1+0_6h','1+200_6h','1+5k_6h','Ctrl_5K_6h','ConA_6h'), IFIT1=c(24,25,24.7,24.5,24.2,24.8), stringsAsFactors=FALSE) sub('(^.+)([[:digit:]]h$)', "\\2", xx$Sample) 1] "6h" "6h" "6h" "6h" "6h" "6h" yy<-xx$Sample strsplit(yy,"_") #-------------- [[1]] [1] "Ctrl" "6h" [[2]] [1] "1+0" "6h" [[3]] [1] "1+200" "6h" [[4]] [1] "1+5k" "6h" [[5]] [1] "Ctrl" "5K" "6h" [[6]] [1] "ConA" "6h" Try instead to use these results to guide you> I have to extract the time information separated by '_' in the > sample names, > i tried grep and strsplit, it looks that i am not providing some > information > correctly. I would appreciate if someone can point me to the correct > way. > Thanks > Sharad > > -- > View this message in context: http://r.789695.n4.nabble.com/splitting-sample-names-tp3753712p3753712.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Hi Sharad, Try xx$newSample <- sapply(with(xx, strsplit(as.character(Sample), "_")), "[", 1) xx HTH, Jorge On Aug 18, 2011, at 5:43 PM, 1Rnwb wrote:> I have a data frame like this > xx<-data.frame(cbind(Sample=c('Ctrl_6h','1+0_6h','1+200_6h','1+5k_6h','Ctrl_5K_6h','ConA_6h'), > IFIT1=c(24,25,24.7,24.5,24.2,24.8))) > > grep('[[:digit:]]h',xx$Sample) > > yy<-xx$Sample > > strsplit(yy,"_") > > I have to extract the time information separated by '_' in the sample names, > i tried grep and strsplit, it looks that i am not providing some information > correctly. I would appreciate if someone can point me to the correct way. > Thanks > Sharad > > -- > View this message in context: http://r.789695.n4.nabble.com/splitting-sample-names-tp3753712p3753712.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Try this: transform(xx, Time = gsub(".*_", "", xx$Sample)) On Thu, Aug 18, 2011 at 6:43 PM, 1Rnwb <sbpurohit at gmail.com> wrote:> I have a data frame like this > xx<-data.frame(cbind(Sample=c('Ctrl_6h','1+0_6h','1+200_6h','1+5k_6h','Ctrl_5K_6h','ConA_6h'), > ? ? ? ? ? ? ? ? IFIT1=c(24,25,24.7,24.5,24.2,24.8))) > > grep('[[:digit:]]h',xx$Sample) > > yy<-xx$Sample > > strsplit(yy,"_") > > I have to extract the time information separated by '_' in the sample names, > i tried grep and strsplit, it looks that i am not providing some information > correctly. I would appreciate if someone can point me to the correct way. > Thanks > Sharad > > -- > View this message in context: http://r.789695.n4.nabble.com/splitting-sample-names-tp3753712p3753712.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O