subsetting a dataframe in R - unexpected results

大兔子大兔子 提交于 2019-12-02 11:26:00

问题


OK, couldn't find a better title

Let's say I have my_dataframe:

Name Value1 Value2
AA    10     20
BB    15     30

if I do: nrow(my_dataframe[my_dataframe$Value2>20,] I get '1' as result

I want to create my_second_dataframe, such as there's only column 'Value2':

my_second_dataframe<- my_dataframe[,'Value2', drop=FALSE]

let me check it out:

class(my_second_dataframe)
[1] "data.frame"
class(my_second_dataframe$Value2)
[1] "numeric"

but then:

nrow(my_second_dataframe[my_second_dataframe$Value2>20,]
NULL

????? This would be part of a function, in which I want to isolate a column of choice and also get number of rows of that column based on a threshold number. What am I doing wrong?

Thanks


回答1:


Based on the documentation in ?Extract

drop : For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.

Also, by default it is drop = TRUE for [

x[i, j, ... , drop = TRUE]

So, we need to specify drop = FALSE to avoid coercing to lowest possible dimension when there is only a single column or row.

In the OP's example

my_second_dataframe[my_second_dataframe$Value2>20,, drop=FALSE]


来源:https://stackoverflow.com/questions/33596701/subsetting-a-dataframe-in-r-unexpected-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!