这可能是非常基本的(抱歉),但我是编码初学者,无法在网上找到说明。我使用 R 并有一个像这样的数据框:
id<-c(1:4)
happy<-c(1,0,1,0)
sad<-c(0,0,0,1)
angry<-c(0,1,0,0)
excited<-c(1,0,1,1)
emot<-data.frame(id, happy, sad, angry, excited)
emot
id happy sad angry excited
1 1 0 0 1
2 0 0 1 0
3 1 0 0 1
4 0 1 0 1
id 表示一个人,其他变量表示该人是否提到某种情绪(1)或没有(0)。我想转换数据框以获得这个结果:
source target count
happy sad 0
happy angry 0
happy excited 2
sad angry 0
sad excited 1
angry excited 0
我确实尝试过表格功能,但没有成功。预先感谢您!
在 R 基础中,你可以做类似的事情:
combo <- combn(names(emot)[-1], 2)
t(combn(names(emot)[-1], 2)) |>
as.data.frame() |>
setNames(c("target", "Source")) |>
transform(count = sapply(asplit(combo, 2), \(x) sum(rowSums(emot[,x]) == length(x))))
输出
target Source count
1 happy sad 0
2 happy angry 0
3 happy excited 2
4 sad angry 0
5 sad excited 1
6 angry excited 0