如何使用data.frame中的“which”来选择数据

时间:2016-04-07 04:47:23

标签: r dataframe which

这是一个简单的示例代码:

> newdata <- mtcars[which(mtcars$cyl > 4)]

但不幸的是我收到了这样的错误:

Error in `[.data.frame`(mtcars, which(mtcars$cyl > 4)) : 
  undefined columns selected

请帮我弄清楚如何在data.frame中使用which

1 个答案:

答案 0 :(得分:1)

除非有一些NA值,否则我们不需要用which包裹它。通常,行/列索引是行索引在,的lhs上,列索引在,的rhs上。但是,默认情况下,如果我们不使用任何,,则会将其视为列索引。

 mtcars[mtcars$cyl>4,]