[R] strsplit

Sarah Goslee sarah.goslee at gmail.com
Tue Jun 1 16:05:36 CEST 2010


How about this:

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

# for one
unlist(strsplit(testdata[1,1], split="_"))

# for all of them
do.call(rbind, sapply(testdata[,1], strsplit, split="_"))


Sarah

On Tue, Jun 1, 2010 at 5:45 AM, Joël Baumann <joelbaumann at gmx.net> wrote:
> 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!
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list