简体   繁体   中英

R: extract info from the data frame with row names

I want to extract specific information from the data frame, but can't extract row names too.

My dummy example:

dummy <- as.data.frame(matrix(c(34,11,9,32,23,13), ncol=2))
colnames(dummy) <- c('C1', 'C2')
rownames(dummy) <- c('Row1', 'Row2', 'Row3')

dummy
   C1 C2
Row1 34 32
Row2 11 23
Row3  9 13

I want to extract C2 from the dummy . I tried:

dummy$C2
[1] 32 23 13

as.data.frame(dummy$C2)
  dummy$C2
1       32
2       23
3       13

subset(dummy)$C2
[1] 32 23 13

How can I extract row names too, for the result like this:

dummy_extracted

Row1       32
Row2       23
Row3       13

Try

dummy[, "C2", drop = FALSE]

     C2
Row1 32
Row2 23
Row3 13

With ?subset()

 subset(dummy, select=C2)
      C2
 Row1 32
 Row2 23
 Row3 13

If you look at the documentation of ?subset(), by default:

 ## S3 method for class 'data.frame'
 subset(x, subset, select, drop = FALSE, ...)

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