Try, sID <- c("a", "1,2,3", "b", "4,5,6")
tmp1 <- strsplit(sID,',') tmp2 <- lapply(tmp1, function(x) if (length(x)==1) c('','',x) else x ) tmp3 <- matrix(unlist(tmp2),ncol=3, byrow=TRUE) rID <- c("shr1125", "bwr331", "bwr330", "vjhr1022") newdf <- data.frame(cbind(tmp3,rID)) You'll need to name the first three columns. As an aside, note that you don't need the cbind in your data.frame(cbind(sID,rID)) because data.frame(sID,rID) does just as well. But cbind is needed in my example, because tmp3 is a matrix. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 8/13/13 12:09 PM, "Steven Ranney" <steven.ran...@gmail.com> wrote: >I have a dataFrame > >sID <- c("a", "1,2,3", "b", "4,5,6") >rID <- c("shr1125", "bwr331", "bwr330", "vjhr1022") > >tmp <- data.frame(cbind(sID,rID)) > >but I need to split tmp$sID into three different columns, filling >locations >where tmp$sID has only one value with NA. > >I can split tmp$sID by the comma > >tmp.1 <- strsplit(tmp$sID, ",") > >but I can't figure out how to convert the resulting list into a dataFrame. > >Ideally, tmp will become four columns wide, something like > >sID.a sID.b sID.c rID >NA NA a shr1125 >1 2 3 bwr331 >NA NA b bwr330 >4 5 6 vjhr1022 > >Thoughts or suggestions? > >I tried > >havecomma - grep(',', tmp$sID) > >for( i in 1:nrow(tmp)){ > if (!(tmp[i,] %in% havecomma)){ > tmp$sID[i] <- paste(', ,', tmp$sID[i], sep="") > } > } > >and thought that I might be able to force the list into a dataframe once >each component had three items, but it just seemed to apply the paste() >function to everything which gave me a list with varying numbers of items. > >I'm stuck. > >Thanks for your help - > >SR > > > > > > >Steven H. Ranney > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@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. ______________________________________________ R-help@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.