我在R中有da数据帧,看起来像这样:
Name Online
Paul Y
Toni Y
Mike N
Tim Y
Mark N
Paul N
Mike Y
Tim Y
Mark N
Paul Y
现在我想过滤所有行,其中Name在Online列中至少包含一次“Y”和一次“N”。因此,对于该示例,我的结果应该如下:
Name Online
Paul Y
Mike N
Paul N
Mike Y
Paul Y
你知道我怎么能在R中做到这一点吗? 非常感谢
代码:
Name <- c("Paul","Toni","Mike","Tim","Mark","Paul","Mike","Tim","Mark","Paul")
Online <- c("Y","Y","N","Y","N","N","Y","Y","N","Y")
df <- data.frame(Name, Online)
答案 0 :(得分:1)
y <- df[df$Online == "Y", "Name"]
n <- df[df$Online == "N", "Name"]
df[df$Name %in% intersect(y,n), ]
答案 1 :(得分:1)
你可以这样做:
with(df, intersect(Name[Online == 'Y'], Name[Online == 'N']))
#"Paul" "Mike"
为您提供满足条件的名称,然后您可以使用%in%
选择df
中满足条件的行
subset(df, Name %in% intersect(Name[Online == 'Y'], Name[Online == 'N'])))
# Name Online
#1 Paul Y
#3 Mike N
#6 Paul N
#7 Mike Y
#10 Paul Y
这里with
是不必要的,因为subset
已经在数据框的上下文中评估了它的第二个参数。
答案 2 :(得分:1)
我试图给你一个可以为任何数据帧运行的通用代码。(这里没有任何逻辑,只是聪明的方式---)
Name <- c("Paul","Toni","Mike","Tim","Mark","Paul","Mike","Tim","Mark","Paul")
Online <- c("Y","Y","N","Y","N","N","Y","Y","N","Y")
df <- data.frame(Name, Online)
df$new=paste(df[,1],df[,2],sep = "_")
f=unique(df)
f=f[,1:2]
You can use this code just by changing column number.
Hope this will help.
答案 3 :(得分:0)
使用data.table包
myTable = data.table(myFrame)
bothNames = myTable[(Name %in% myTable[Online == "Y"]$Name) & (Name %in% myTable[Online == "N"]$Name)]