[R] splitting scientific names into genus, species, and sub
(Ted Harding)
Ted.Harding at manchester.ac.uk
Wed Nov 4 23:15:53 CET 2009
OPPS^2!! Did it again. The version given below now does seem to work
properly: last line now changed (yet again) to
A$Count <- as.numeric(levels(A$Count)[unclass(A$Count)])
On 04-Nov-09 21:47:32, Ted Harding wrote:
> OOPS! Sorry, I made an oversight in the code I posted just now
> (and I didn't check the result carefullt enough ... ).
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)[unclass(A$Count)])
A
# 1 genusA speciesA no.subspecies 10
# 2 genusB speciesAA no.subspecies 20
# 3 genusC speciesAAA subspeciesA 15
# 4 genusC speciesAAA subspeciesB 25
Ted
[I plead hypocaffeinaemia]
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Nov-09 Time: 22:15:50
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list