user3605092
user3605092

Reputation: 11

Extract subset of data

Ok, I have a matrix of values with certain identifiers, such as:

A  2
B  3
C  4
D  5
E  6
F  7
G  8

I would like to pull out a subset of these values (using R) based on a list of the identifiers ("B", "D", "E") for example, so I would get the following output:

B  3
D  5
E  6

I'm sure there's an easy way to do this (some sort of apply?) but I can't seem to figure it out. Any ideas? Thanks!

Upvotes: 0

Views: 106

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99361

Your matrix:

> m <- matrix(2:8, dimnames = list(LETTERS[1:7]))

You can use %in% to filter out the desired rows. If the original matrix only has a single column, using drop = FALSE will keep the matrix structure. Otherwise it will be converted to a named vector.

> m[rownames(m) %in% c("B", "D", "E"), , drop = FALSE]
#   [,1]
# B    3
# D    5
# E    6

Upvotes: 0

jlhoward
jlhoward

Reputation: 59415

If the letters are the row names, then you can just use this:

m <- matrix(2:8, dimnames = list(LETTERS[1:7], NULL))
m[c("B","D","E"),]
# B D E 
# 3 5 6 

Note that there is a subtle but very important difference between: m[c("B","D","E"),] and m[rownames(m) %in% c("B","D","E"),]. Both return the same rows, but not necessarily in the same order.

The former uses the character vector c("B","D","E") as in index into m. As a result, the rows will be returned in the order of character vector. For instance:

# result depends on order in c(...)
m[c("B","D","E"),]
# B D E 
# 3 5 6 
m[c("E","D","B"),]
# E D B 
# 6 5 3 

The second method, using %in%, creates a logical vector with length = nrow(m). For each element, that element is T if the row name is present in c("B","D","E"), and F otherwise. Indexing with a logical vector returns rows in the original order:

# result does NOT depend on order in c(...)
m[rownames(m) %in% c("B","D","E"),]
# B D E 
# 3 5 6 
m[rownames(m) %in% c("E","D","B"),]
# B D E 
# 3 5 6 

This is probably more than you wanted to know...

Upvotes: 1

Related Questions