r - 如何根据(元素)选定的相邻列计算重复的行数
问题描述
我有一个数据框 测试:
group userID A_conf A_chall B_conf B_chall
1 220 1 1 1 2
1 222 4 6 4 4
2 223 6 5 3 2
1 224 1 5 4 4
2 228 4 4 4 4
数据包含每个用户的响应(由 userID 显示),其中每个用户可以为两个度量输入 1 到 6 之间的任何值:
- 会议
- 挑战
他们也可以选择不响应,从而导致NA条目。
测试数据框包含几列,如 A、B、C、D 等。可以为这些列中的每一个单独报告 Conf 和 Chall 度量。
我有兴趣进行以下比较:
- A_conf & A_chall
- B_conf & B_chall
如果这些测量值中的任何一个相等,则最终计数器应递增(如下所示)。
group userID A_conf A_chall B_conf B_chall Final
1 220 1 1 1 2 1
1 222 4 6 4 4 1
2 223 6 5 3 2 0
1 224 1 5 4 4 1
2 228 4 4 4 4 2
我在决赛柜台上挣扎。什么脚本可以帮助我实现这个功能?
供参考,测试数据帧集的 dput 共享如下:
输入(测试):
结构(列表(组= c(1L,1L,2L,1L,2L),
用户 ID = c(220L, 222L, 223L, 224L, 228L),
A_conf = c(1L, 4L, 6L, 1L, 4L),
A_chall = c(1L, 6L, 5L, 5L, 4L),
B_conf = c(1L, 4L, 3L, 4L, 4L),
B_chall = c(2L, 4L, 2L, 4L, 4L)),
类 = “data.frame”, row.names = c(NA, -5L))
我尝试了这样的代码:
test$Final = as.integer(0) # add a column to keep counts
count_inc = as.integer(0) # counter variable to increment in steps of 1
for (i in 1:nrow(test)) {
count_inc = 0
if(!is.na(test$A_conf[i] == test$A_chall[i]))
{
count_inc = 1
test$Final[i] = count_inc
}#if
else if(!is.na(test$A_conf[i] != test$A_chall[i]))
{
count_inc = 0
test$Final[i] = count_inc
}#else if
}#for
上面的代码只在A_conf和A_chall列上工作。问题是,无论输入的值(由用户)是否相等,它都会用全 1填充Final列。
解决方案
假设您具有相同数量的“conf”和“chall”列的基本 R 解决方案
#Find indexes of "conf" column
conf_col <- grep("conf", names(test))
#Find indexes of "chall" column
chall_col <- grep("chall", names(test))
#compare element wise and take row wise sum
test$Final <- rowSums(test[conf_col] == test[chall_col])
test
# group userID A_conf A_chall B_conf B_chall Final
#1 1 220 1 1 1 2 1
#2 1 222 4 6 4 4 1
#3 2 223 6 5 3 2 0
#4 1 224 1 5 4 4 1
#5 2 228 4 4 4 4 2
也可以单线完成
rowSums(test[grep("conf", names(test))] == test[grep("chall", names(test))])
推荐阅读
- node.js - npm 的非确定性行为
- visual-studio-code - 如何在 Emmet 中为 BEM 功能设置 VS Code 编辑器?
- token - 延长访问令牌寿命的身份机制
- amazon-web-services - 未使用 AWS EC2 IAM 角色
- php - php stream_select 正在返回没有什么可读取的流
- xcode - 为什么 Xcode 总是在 ~ 1 小时 8 分钟后关闭运行 100% 的 CPU?
- c# - C# 如何将序列化的 .NET 对象编译为 .NET 程序集
- c# - 可以使 IntelliSense 不区分重音吗?
- java - 通过捆绑在片段之间传递值
- webview - 在电子的另一个webview中打开一个webview的devtools?