Re: [R] strsplit

From: Ista Zahn <istazahn_at_gmail.com>
Date: Tue, 01 Jun 2010 10:20:01 -0400

Another option is to use the colsplit() function (in the reshape package):

testdata <- data.frame(sp = c("GenusA_SpeciesC_Tree", "GenusA_SpeciesF_Tree", "GenusB_SpeciesA_Shrub"), stringsAsFactors=FALSE)

library(reshape)
testdata <- cbind(testdata, colsplit(testdata$sp, split="_", names=c("Genus", "Species", "Type")))

-Ista

On Tuesday 01 June 2010 10:10:22 am Ivan Calandra wrote:
> Hi,
>
> I don't know if it would help you since your goal is not really clear to
>
> me, but here are some thoughts:
> > x <- c("GenusA_SpeciesC_Tree", "GenusA_SpeciesF_Tree",
>
> "GenusB_SpeciesA_Shrub")
>
> > test <- strsplit(x, "_")
> > test
>
> [[1]]
> [1] "GenusA" "SpeciesC" "Tree"
> [[2]]
> [1] "GenusA" "SpeciesF" "Tree"
> [[3]]
> [1] "GenusB" "SpeciesA" "Shrub"
>
> > lapply(test, "[", 1)
>
> [[1]]
> [1] "GenusA"
> [[2]]
> [1] "GenusA"
> [[3]]
> [1] "GenusB"
>
> > df <- data.frame(Genus=unlist(lapply(test,"[",1)),
>
> Species=unlist(lapply(test,"[",2)), Veget=unlist(lapply(test,"[",3)))
>
> > df
>
> Genus Species Veget
> 1 GenusA SpeciesC Tree
> 2 GenusA SpeciesF Tree
> 3 GenusB SpeciesA Shrub
>
> There are probably better and nicer ways to do each step.
>
> HTH,
> Ivan
>
> Le 6/1/2010 11:45, Jol Baumann a crit :
> > Hello!
> >
> > I have the following problem:
> >
> > I have a file in R that has in the first row three informations in one
> > row that I would like to in three different rows.
> >
> > The first row looks like this:
> >
> > GenusA_SpeciesC_Tree
> > GenusA_SpeciesF_Tree
> > GenusB_SpeciesA_Shrub
> > ...
> >
> > I tried with strsplit and and substring but I don't get any solution.
> > I know I can do this in Excel, but in R would be much nicer!
> >
> > Thanks for helping me.
> >
> > Jol Baumann
> >
> > ______________________________________________
> > 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.



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. Received on Tue 01 Jun 2010 - 14:20:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 14:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive