[R] strsplit

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue Jun 1 16:10:22 CEST 2010


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, Joël 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.
>
> Joël 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.
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php



More information about the R-help mailing list