提取其他两列具有相同值对的数据帧行

时间:2017-10-21 12:59:38

标签: r dataframe

我试图提取X1的值,其中X2和X3是同一对值。

  X1 X2  X3
1  1  1   8
2  2  4   8
3  3  4   8
4  4 90 199
5  5  3   9 
6  6  3   9

我可以发出一个命令

subset(df, df$X2==4 & df$X3==8)

将返回我

  X1 X2 X3
2  2  4  8
3  3  4  8

但我怎样才能让它充满活力?所以它返回

  X1 X2 X3
2  2  4  8
3  3  4  8
5  5  3  9 
6  6  3  9

2 个答案:

答案 0 :(得分:1)

使用lambda k: k执行此操作的方法。计算每对(X2,X3)的出现次数,多次出现的过滤对,将它们连接到初始data.frame:

dplyr

答案 1 :(得分:0)

INPUT

df <- data.frame(x1 = c(1,2,3,5,4,6), x2 = c(3,4,5,3,6,6), x3 = c(4,4,2,4,2,2))
df
#   x1 x2 x3
# 1  1  3  4
# 2  2  4  4
# 3  3  5  2
# 4  5  3  4
# 5  4  6  2
# 6  6  6  2

Paired x2 and x3

Creating df1 with x1 and pair of x2, x3 values as one column

df1 <- data.frame(x1 = df$x1, pair = paste(df$x2,df$x3, sep = ","))
df1
#   x1 pair
# 1  1  3,4
# 2  2  4,4
# 3  3  5,2
# 4  5  3,4
# 5  4  6,2
# 6  6  6,2

Required Output

Extracting rows from df where the frequency of a pair is greater than 1, i.e. if any x2, x3 pair has frequency greater than 1 those pairs would be extracted from df with the help fo df1

df[df1$pair %in% names(which(table(df1$pair) > 1)),]
#   x1 x2 x3
# 1  1  3  4
# 4  5  3  4
# 5  4  6  2
# 6  6  6  2