Here's a slightly different approach without regular expressions: > dat1<-data.frame(slope=c("slope (60/25/15)","slope (90/10)", "slope (40/35/15/10)","slope (40/25/25/10)" ))
> tmp <- strsplit(as.character(dat1$slope), split="[/()]") > tmp2 <- sapply(tmp, function(x) as.numeric(x[-1])) > maxlen <- max(sapply(tmp2, length)) > datnew <- data.frame(t(sapply(tmp2, function(x) c(x, rep(0, maxlen-length(x)))))) > datnew X1 X2 X3 X4 1 60 25 15 0 2 90 10 0 0 3 40 35 15 10 4 40 25 25 10 ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of arun > Sent: Friday, August 17, 2012 6:56 AM > To: Sapana Lohani > Cc: R help > Subject: Re: [R] Unequal splits from a column > > HI, > Try this: > dat1<-data.frame(slope=c("slope (60/25/15)","slope (90/10)","slope > (40/35/15/10)","slope (40/25/25/10)" )) > dat1$slope<-gsub("slope\\s+.*(\\(.*\\))","\\1",dat1$slope) > dat1$slope<-gsub("\\((.*)\\)","\\1",dat1$slope) > dat2<-strsplit(dat1$slope,"/") > dat2[[1]][4]<-0 > dat2[[2]][3:4]<-0 > data.frame(do.call(rbind,dat2)) > # X1 X2 X3 X4 > #1 60 25 15 0 > #2 90 10 0 0 > #3 40 35 15 10 > #4 40 25 25 10 > > > > ----- Original Message ----- > From: Sapana Lohani <lohani.sap...@ymail.com> > To: "r-help@r-project.org" <r-help@r-project.org> > Cc: > Sent: Friday, August 17, 2012 1:05 AM > Subject: [R] Unequal splits from a column > > Hi I am new to R so am struggling with the commands here > > I have one column in a table that looks like > > > slope (60/25/15) > slope (90/10) > slope (40/35/15/10) > slope (40/25/25/10) > I want to have 4 columns with just the number inside the parenthesis. > when there is no number that cell can have 0. I want the output like > this > > 60 25 15 0 > 90 10 0 0 > 40 35 15 10 > 40 25 25 10 > > Can somebody help me?? > Thanks > [[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. ______________________________________________ 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.