Reputation: 28339
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
Upvotes: 1
Views: 204
Reputation: 887158
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, ...)
Upvotes: 1