I have a data frame with unique row names. I want to create a subset data frame with single row based on the the row names. When I am using data[rownames(data)==name, ] I am not getting a data frame instead getting a value vector. The program is to filter data based on row names and create new data frames.
Asked
Active
Viewed 602 times
1
-
It sounds like you are on the right track, please include a [minimal reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example for more help – Nate Sep 18 '17 at 10:31
2 Answers
7
We need drop = FALSE as by default, the [ have drop = TRUE. So, if there is a single row, then it gets converted to lower dimension i.e. a vector.
data[rownames(data)==name, , drop = FALSE ]
akrun
- 874,273
- 37
- 540
- 662
1
You can use subset.
set.seed(4577) # Make it reproducible
dat <- data.frame(A = sample(letters, 10), X = rnorm(10))
subset(dat, subset = rownames(dat) == 3)
# A X
#3 j 0.339270
Rui Barradas
- 70,273
- 8
- 34
- 66