过滤R中的数据帧和不需要的过滤结果 [英] Filtering a data frame in R and an unwanted filtered out result
问题描述
此片段:
名称< -c(Alice,Bob,Charlie)
年龄< -c(25,24,25)
个朋友< -data.frame(名称,年龄)
a25< - 朋友[朋友$ age == 25,]
a25
表(a25 $ names)
给我这个输出
姓名年龄
1爱丽丝25
3查理25
爱丽丝鲍勃查理
1 0 1
现在,为什么Bob在输出中,因为数据帧 a25
不包括Bob?我会期望这样的输出(从表
命令):
Alice Charlie
1 1
我缺少什么?
我的环境:
R版本2.15.2(2012-10-26)
平台:i386-w64-mingw32 / i386(32位)
这个问题似乎在评论中有答案。这个答案有一个额外的方法,并从评论中整理建议。
你所描述的问题如下:你的a25 $名字中没有Bob变量,但是当您使用表
时,Bob显示。这是因为原始列中存在的级别已被保留。
table(a25 $ names)
#
#Alice Bob Charlie
#1 0 1
幸运的是,有一个名为#
#爱丽丝查理
#1 1
等级数组
函数也可以在 data.frame
上工作,可以执行以下操作: p>
a25alt< - studvels(friends [friends $ ages == 25,])
a25alt
#名称年龄
#1爱丽丝25
#3查理25
表(a25alt $名称)
#
#爱丽丝查理
#1 1
正如评论中所提到的,还看看 as.character
和因素
:
table(as.character(a25 $ names))
表(factor(a25 $ names))
This snippet:
names<-c("Alice","Bob","Charlie")
ages<-c(25,24,25)
friends<-data.frame(names,ages)
a25 <- friends[friends$age==25,]
a25
table(a25$names)
gives me this output
names ages
1 Alice 25
3 Charlie 25
Alice Bob Charlie
1 0 1
Now, why "Bob" is in the output since the data frame a25
does not include "Bob"? I would expected an output like this (from the table
command):
Alice Charlie
1 1
What am I missing?
My environment:
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)
This question appears to have an answer in the comments. This answer shares one additional approach and consolidates the suggestions from the comments.
The problem you describe is as follows: There is no "Bob" in your "a25$names" variable, but when you use table
, "Bob" shows up. This is because the levels present in the original column have been retained.
table(a25$names)
#
# Alice Bob Charlie
# 1 0 1
Fortunately, there's a function called droplevels
that takes care of situations like this:
table(droplevels(a25$names))
#
# Alice Charlie
# 1 1
The droplevels
function can work on a data.frame
too, allowing you to do the following:
a25alt <- droplevels(friends[friends$ages==25,])
a25alt
# names ages
# 1 Alice 25
# 3 Charlie 25
table(a25alt$names)
#
# Alice Charlie
# 1 1
As mentioned in the comments, also look at as.character
and factor
:
table(as.character(a25$names))
table(factor(a25$names))
这篇关于过滤R中的数据帧和不需要的过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!