r - 独特读者之间的比较
问题描述
代表
dat <- data.frame(id = c(1,1,2,2,3,3,4,4),
reader = c(1,4,2,3,3,4,2,5),
response = c("CR","PR","SD","SD","PR","PR","CR","SD"))
问题:希望response
在每个独特之处reader
进行比较id
。总共有 5 个独特readers
的,但每个id
只有 2 个个体readers
。
生成的数据集将如下所示:
# A tibble: 4 x 4
id read1 read2 matchflag
<dbl> <chr> <chr> <dbl>
1 1 CR PR 0
2 2 SD SD 1
3 3 PR PR 1
4 4 CR SD 0
解决方案
一个data.table
选项
dcast(
setDT(df),
id ~ paste0("reader", rowid(id)),
value.var = "response"
)[
,
match_flag := +(reader1 == reader2)
][]
给
id reader1 reader2 match_flag
1: 1 CR PR 0
2: 2 SD SD 1
3: 3 PR PR 1
4: 4 CR SD 0
推荐阅读
- python - python中对象和方法的递归
- database - 在 Couchbase 中使用 N1QL 从对象对象中获取所有唯一键
- python - 将 LIVE SPEECH 与 Tkinter GUI 连接
- machine-learning - 如何在分类中获取类名?
- javascript - 从两个字段计算值并显示在 JavaScript 的第三个字段中
- machine-learning - 我将如何计算非线性(rbf 内核)svm 中距超平面/决策边界的边距距离?
- flutter - 如何在没有 Cupertino 底栏的情况下路由到新页面
- c# - 带有 TextWrap 的单元格行不会使用 EPPlus 自动调整大小
- architecture - 构建身份验证服务所需的架构
- tensorflow - 名称 tf.Session 已弃用。请改用 tf.compat.v1.Session