[R] splitting scientific names into genus, species, and sub

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Nov 4 22:47:32 CET 2009


OOPS! Sorry, I made an oversight in the code I posted just now
(and I didn't check the result carefullt enough ... ).

The line which was A$Count <- as.numeric(A$Count) should have
been A$Count <- as.numeric(levels(A$Count)) (i.e. I overlooked
that A$Count as first constructed is a *factor*)!

So the full corrected code is as follows:

a <- matrix(c('genusA speciesA', 10,
              'genusB speciesAA', 20,   
              'genusC speciesAAA subspeciesA', 15, 
              'genusC speciesAAA subspeciesB', 25), nrow=4, byrow=TRUE)

a
#      [,1]                            [,2]
# [1,] "genusA speciesA"               "10"
# [2,] "genusB speciesAA"              "20"
# [3,] "genusC speciesAAA subspeciesA" "15"
# [4,] "genusC speciesAAA subspeciesB" "25"

A <- NULL
for( i in (1:nrow(a))){
  Names <- unlist(strsplit(a[i,1],"[ ]+"))
  if(length(Names)==2) Names <- c(Names,"no.subspecies")
  A <- rbind(A,c(Names,a[i,2]))
}
colnames(A) <- c("Genus","Species","Subspecies","Count")
A <- as.data.frame(A)
A$Count <- as.numeric(levels(A$Count))

A
#    Genus    Species    Subspecies Count
# 1 genusA   speciesA no.subspecies    10
# 2 genusB  speciesAA no.subspecies    15
# 3 genusC speciesAAA   subspeciesA    20
# 4 genusC speciesAAA   subspeciesB    25

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Nov-09                                       Time: 21:47:29
------------------------------ XFMail ------------------------------




More information about the R-help mailing list