简体   繁体   中英

Extract Observation from a Matrix

I have a matrix (see above). I have the name of species and sub-species of plants in rows.

I would like to generate the same matrix with only the species and a matrix with only the sub-species.

The species in my initial matrix are composed of one word ( abelia , abis ) and sub-species contain always two words ( abies alba , etc).

How can i do that in R ?

Assuming that the matrix is called m , you can try this:

species_rows <- lengths(strsplit(rownames(m)," "))==1 #split the rownames at whitespaces, retain only rows that are not split (vector of length 1).
species_mat <- m[species_rows,] #logical subsetting
subspecies_mat <- m[!species_rows,] #logical subsetting with negation

Hat tip to @akrun for pointing out that lapply(..,length) can be replaced by lengths() .


Or even simpler:

species_rows <- !grepl(" ",rownames(m)) # does the row.name NOT contain a whitespace? (TRUE / FALSE)
species_mat <- m[species_rows,]
subspecies_mat <- m[!species_rows,]

Welcome to SO, as suggested, it would have been good if you provided sample data to your question.

That said, I think you can do:

# First, generate data:
a <- matrix(sample(c(0, 1), 20), ncol = 4)
rownames(a) <- c("abies", 
                 "abies alba", 
                 "abies amabilis", 
                 "abies balsamea", 
                 "abies concolor")

Then, you can use grep to find which names contain an empty space:

sp <- grep(" ", rownames(a))

And finally, assign to new matrixes:

subspecies <- a[sp,]
species <- a[-sp,]

As a side note, I'd recommend working with a data frame instead of a matrix, and assigning the names to a variable, instead of rownames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM