[R] Extract a submatrix from matrix

# create matrix
mat <- warpbreaks[1:10,]
mat

Output:
 breaks wool tension
1 26 A L
2 30 A L
3 54 A L
4 25 A L
5 70 A L
6 52 A L
7 51 A L
8 26 A L
9 67 A L
10 18 A M


# create submatrix
submat <- mat[rownames(mat) %in% c(1,2,10) , colnames(mat) %in% c("breaks","tension")]
submat


Output: 
breaks tension
1 26 L
2 30 L
10 18 M

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s