[R] Stringr Package

arun smartpink111 at yahoo.com
Wed Apr 10 20:02:26 CEST 2013


Hi,
Just noticed that there is space before and after the "-"
dat1 <- read.table(text = ' X    Y
ab    "su - di"
ac    "pi - tu"
ad    "tu - tu"', header=TRUE,stringsAsFactors=FALSE)

library(stringr)
  Y<-str_trim(unlist(str_split(dat$Y,"-")),side="both")
 X<-unlist(str_split(str_trim(str_dup(str_pad(dat1$X,width=3,side="right"),2))," "))
data.frame(X,Y,stringsAsFactors=FALSE)
 #  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: arun <smartpink111 at yahoo.com>
To: Sudip Chatterjee <sudipanalyst at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, April 10, 2013 1:24 PM
Subject: Re: [R] Stringr Package

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.



More information about the R-help mailing list