Hi Group, I have a question on Stringr package I have a table like this X Y ab su - di ac pi - tu ad tu - tu I want output like this X Y ab su ab di ac pi ac tu ad tu ad tu I am wondering if this operation can be done using stringr package (only) ? [[alternative HTML version deleted]]
Hi, Assuming that this is a data.frame. dat1<-read.table(text=" X,Y ab,su-di ac,pi-tu ad,tu-tu ",sep=",",header=TRUE,stringsAsFactors=FALSE) library(stringr) dat2<-data.frame(X=rep(dat1$X,each=2),Y= unlist(str_split(dat1$Y,"-")),stringsAsFactors=FALSE) ?dat2 #?? X? Y #1 ab su #2 ab di #3 ac pi #4 ac tu #5 ad tu #6 ad tu A.K. ----- Original Message ----- From: Sudip Chatterjee <sudipanalyst at gmail.com> To: R help <r-help at r-project.org> Cc: Sent: Wednesday, April 10, 2013 12:25 PM Subject: [R] Stringr Package Hi Group, I have a question on Stringr package I have a table like this X? ? Y ab? ? su - di ac? ? pi - tu ad? ? tu - tu I want output like this ? X? ? ? ? Y ab? ? ? ? su ab? ? ? ? di ac? ? ? ? pi ac? ? ? ? tu ad? ? ? ? tu ad? ? ? ? tu I am wondering if this operation can be done using stringr package (only) ? ??? [[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 Wed, Apr 10, 2013 at 12:25 PM, Sudip Chatterjee <sudipanalyst at gmail.com> wrote:> Hi Group, > > I have a question on Stringr package > > I have a table like this > X Y > ab su - di > ac pi - tu > ad tu - tu > > I want output like this > X Y > ab su > ab di > ac pi > ac tu > ad tu > ad tu > > I am wondering if this operation can be done using stringr package (only)I doubt it. stringr manipulates strings, you also want to reshape the data. You can do dat <- read.table(text = ' X Y ab "su - di" ac "pi - tu" ad "tu - tu"', header=TRUE) library(stringr) with(dat, data.frame(X=rep(X, each=2), Y=unlist(strsplit(Y, split=" - ")))) but 'with', 'rep', 'data.frame' and 'unlist' are all in the base package, not stringr. Best, Ista> ? > > [[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.