0

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.

pogibas
  • 25,773
  • 19
  • 74
  • 108
Karthik
  • 25
  • 3
  • 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 Answers2

6

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
  • 789,025
  • 32
  • 460
  • 575
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
  • 57,195
  • 8
  • 29
  • 57