我有一个名为 myTable
的表(输入):
user_name session_num
1 "Joe" 1
2 "Tom" 2
3 "Fred" 1
4 "Tom" 1
5 "Joe" 2
6 "John" 1
我想知道我的 user_id
中有多少人仅session_num = 1
(输出):
user_name session_num
1 "Fred" 1
2 "John" 1
最佳答案
这是使用data.table
的可能解决方案
library(data.table)
setDT(df)[, if(all(session_num == 1)) .SD, by = user_name]
# user_name session_num
# 1: Fred 1
# 2: John 1
另一种选择是尝试反连接
df[session_num == 1][!df[session_num != 1], on = "user_name"]
# user_name session_num
# 1: Fred 1
# 2: John 1
关于r - 排序表唯一参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36059481/