Hi , Does any one know how to split a character vector , I have a vector X that looks like this and each row has 3 characters X ASK DGH ASG AUJ FRT I would like to split the vector into 3 vectors that look like this X1 X2 X3 A S K D G H A S G A U J U R T thanks -- View this message in context: http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939492.html Sent from the R help mailing list archive at Nabble.com.
Dear kayj, Try this: X<-c("ASK", "DGH", "ASG", "AUJ", "FRT") res<-data.frame(do.call(rbind,strsplit(X,""))) colnames(res)<-paste("X",1:ncol(res),sep="") res HTH, Jorge On Tue, Feb 10, 2009 at 1:50 PM, kayj <kjaja27@yahoo.com> wrote:> > Hi , > > > Does any one know how to split a character vector , I have a vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > > thanks > > -- > View this message in context: > http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939492.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Nordlund, Dan (DSHS/RDA)
2009-Feb-10 19:28 UTC
[R] How to split a character vector into 3 vectors
See ?strsplit Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of kayj > Sent: Tuesday, February 10, 2009 10:50 AM > To: r-help at r-project.org > Subject: [R] How to split a character vector into 3 vectors > > > Hi , > > > Does any one know how to split a character vector , I have a > vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > > thanks > > -- > View this message in context: > http://www.nabble.com/How-to-split-a-character-vector-into-3-v > ectors-tp21939492p21939492.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. > >
kayj wrote:> > Hi , > > > Does any one know how to split a character vector , I have a vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > > thanks > >If I understand you correctly, you have this vector: x <- c("ASK", "DGH", "ASG", "AUJ", "URT") This code seems to do what you want: x1 <- substr(x, 1, 1) x2 <- substr(x, 2, 2) x3 <- substr(x, 3, 3) There's probably a much simpler and more elegant way to do it, though. -- View this message in context: http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939521.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2009-Feb-10 20:14 UTC
[R] How to split a character vector into 3 vectors
Another option is: read.table(textConnection(gsub("(\\w)", "\\1;", X)), sep = ";") On Tue, Feb 10, 2009 at 4:50 PM, kayj <kjaja27@yahoo.com> wrote:> > Hi , > > > Does any one know how to split a character vector , I have a vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > > thanks > > -- > View this message in context: > http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939492.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Wacek Kusnierczyk
2009-Feb-10 23:06 UTC
[R] How to split a character vector into 3 vectors
kayj wrote:> Hi , > > > Does any one know how to split a character vector , I have a vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > ># dummy example data n = 3 x = replicate(10, paste(sample(letters, n), collapse="")) y = lapply(1:n, function(i) substr(x, i, i)) # if you need a specific vector x1 = y[[1]] # if you really need all three as separate variables for (i in 1:n) assign(paste('x', i, sep=""), y[[i]]) vQ
Gabor Grothendieck
2009-Feb-11 03:04 UTC
[R] How to split a character vector into 3 vectors
This splits them into a 3 column matrix:> x <- c("ASK", "DGH", "ASG", "AUJ", "URT") > library(gsubfn)> z <- strapply(x, ".", simplify = rbind) > z[,1] [,2] [,3] [1,] "A" "S" "K" [2,] "D" "G" "H" [3,] "A" "S" "G" [4,] "A" "U" "J" [5,] "U" "R" "T" z[,1], z[,2] and z[,3] are your vectors. On Tue, Feb 10, 2009 at 1:50 PM, kayj <kjaja27 at yahoo.com> wrote:> > Hi , > > > Does any one know how to split a character vector , I have a vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > > thanks > > -- > View this message in context: http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939492.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. >
someone has probably answered this already but use substr() new variable<-substr(old.variable,1,1) Simon. ----- Original Message ----- From: "kayj" <kjaja27 at yahoo.com> To: <r-help at r-project.org> Sent: Tuesday, February 10, 2009 6:50 PM Subject: [R] How to split a character vector into 3 vectors> > Hi , > > > Does any one know how to split a character vector , I have a vector X that > looks like this and each row has 3 characters > > X > ASK > DGH > ASG > AUJ > FRT > > I would like to split the vector into 3 vectors that look like this > > X1 X2 X3 > A S K > D G H > A S G > A U J > U R T > > thanks > > -- > View this message in context: > http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939492.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. >