2
$\begingroup$
> age <- c(1,2,3)
> weight <- c(4,5,6)
> p <- data.frame(age=age, height=height)
> p
  age height
1   1      4
2   2      5
3   3      6

> p[p$age > 1,]
  age height
2   2      5
3   3      6

> age
[1] 1 2 3

> age.df <- data.frame(age=age)

> age.df
  age
1   1
2   2
3   3

> age.df[age.df$age > 1,]
[1] 2 3

I want the subset of the dataframe that I asked for, not a vector, in which the rownames and column names are lost. Why does R do this? Can it be prevented?

$\endgroup$
1

1 Answer 1

7
$\begingroup$

Use drop=FALSE:

> age.df[age.df$age > 1, , drop=FALSE]
  age
2   2
3   3

A useful reference is the R Inferno (http://www.burns-stat.com/pages/Tutor/R_inferno.pdf); see section 8.1.44.

$\endgroup$
1
  • $\begingroup$ Thank you! I had been trying drop, but missed the extra comma. $\endgroup$ Dec 7, 2011 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.