在R中对数据帧进行子集 - 意外结果 [英] subsetting a dataframe in R - unexpected results
问题描述
OK,找不到更好的标题
OK, couldn't find a better title
假设我有my_dataframe:
Let's say I have my_dataframe:
Name Value1 Value2
AA 10 20
BB 15 30
如果我这样做:
nrow(my_dataframe [my_dataframe $ Value2> 20,]
我得到'1'作为结果
if I do:
nrow(my_dataframe[my_dataframe$Value2>20,]
I get '1' as result
我想创建my_second_dataframe,例如只有Value2列:
I want to create my_second_dataframe, such as there's only column 'Value2':
my_second_dataframe<- my_dataframe[,'Value2', drop=FALSE]
让我看看: / p>
let me check it out:
class(my_second_dataframe)
[1] "data.frame"
class(my_second_dataframe$Value2)
[1] "numeric"
但是:
nrow(my_second_dataframe[my_second_dataframe$Value2>20,]
NULL
这将是一个函数的一部分,其中我想隔离一列选择,并且还获取该列的行数mn基于阈值数。我做错了什么?
????? 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?
谢谢
推荐答案
根据文件在?提取
drop:对于矩阵和数组。如果为TRUE,则结果被胁迫到
最低可能维度(参见示例)。这只适用于
提取元素,而不是替换。请参阅下一步
的详细信息。
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.
此外,默认情况下,它是 drop = TRUE
Also, by default it is drop = TRUE
for [
x[i, j, ... , drop = TRUE]
所以,我们需要指定 drop = FALSE
,以避免在只有单个列或行时强制为最低可能的维度。
So, we need to specify drop = FALSE
to avoid coercing to lowest possible dimension when there is only a single column or row.
在OP的例子中
my_second_dataframe[my_second_dataframe$Value2>20,, drop=FALSE]
这篇关于在R中对数据帧进行子集 - 意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!