问题描述
我有一个由3个COLS组成的数据帧:
tweet_id | response_id | time
1 2 22:10:47
2 NA 22:10:13
3 1 22:08:27
4 3 21:54:49
5 4 21:49:35
6 5 21:46:23
6 7 21:46:23
8 9 21:30:45
8 6 21:30:45
8 10 21:30:45
我希望将tweet_id与Response_id进行比较--但我希望遍历所有tweet_id作为引用,并查看在tweet_id中是否存在Response_id值。如果不是,请删除该行。
您会注意到tweet_id和time中存在重复项--根据此逻辑,理想情况下它们应该是唯一的。
已尝试筛选(tweet_id!=Response_tweet_id),但不起作用。
所需输出:
tweet_id | response_id | time
1 2 22:10:47
3 1 22:08:27
4 3 21:54:49
5 4 21:49:35
6 5 21:46:23
8 6 21:30:45
推荐答案
您可以使用%in%
选择response_id
出现在tweet_id
中的行。
subset(df, response_id %in% unique(tweet_id))
# tweet_id response_id time
#1 1 2 22:10:47
#2 3 1 22:08:27
#3 4 3 21:54:49
#4 5 4 21:49:35
#5 6 5 21:46:23
#6 8 6 21:30:45
如果要使用dplyr
library(dplyr)
df %>% filter(response_id %in% unique(tweet_id))