我的数据看起来像
id camera
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam03
angelina cam22
barry cam03
barry cam03
barry cam03
barry cam03
barry cam03
barry cam03
barry cam03
barry cam03
barry cam03
barry cam03
barry cam22
barry cam22
barry cam22
barry cam22
barry cam22
barry cam15
barry cam25
因此,每个人都被记录在每个摄像机中,我想知道每对摄像机中有多少人,因此对于摄影机1和2,在这两个摄像机中记录了多少个人。在上面,在摄像机1和2中只能看到个人A,对于摄像机1和3,在它们两者中都可以看到单独的B和E,所以我想要的结果就像一张桌子
0001 0002 0003
0001 - 1 2
0002 1 - 0
0003 2 0 -
如果有人能在R
中向我展示这个代码,我将不胜感激答案 0 :(得分:3)
crossprod
应该这样做:
crossprod(table(mydf))
# Camera
# Camera 0001 0002 0003
# 0001 4 1 2
# 0002 1 1 0
# 0003 2 0 3
如果需要, diag
可用于将对角线设置为零或NA
。您可以一次性完成所有操作:
`diag<-`(crossprod(table(mydf)), 0)
# Camera
# Camera 0001 0002 0003
# 0001 0 1 2
# 0002 1 0 0
# 0003 2 0 0
示例数据:
mydf <- data.frame(
Individual = c("A", "A", "B", "B", "C", "D", "E", "E"),
Camera = c("0001", "0002", "0001", "0003", "0001", "0003", "0001", "0003"))
编辑:
如果同一个人的相机重复,您可以在crossprod
来电之前消除重复:
`diag<-`(crossprod(table(unique(mydf2))), 0)
camera
#camera cam03 cam15 cam22 cam25
#cam03 0 1 2 1
#cam15 1 0 1 1
#cam22 2 1 0 1
#cam25 1 1 1 0
第二个数据:
mydf2 <- structure(list(id = c("angelina", "angelina", "angelina", "angelina",
"angelina", "angelina", "angelina", "angelina", "angelina", "angelina",
"angelina", "angelina", "barry", "barry", "barry", "barry", "barry",
"barry", "barry", "barry", "barry", "barry", "barry", "barry",
"barry", "barry", "barry", "barry", "barry"), camera = c("cam03",
"cam03", "cam03", "cam03", "cam03", "cam03", "cam03", "cam03",
"cam03", "cam03", "cam03", "cam22", "cam03", "cam03", "cam03",
"cam03", "cam03", "cam03", "cam03", "cam03", "cam03", "cam03",
"cam22", "cam22", "cam22", "cam22", "cam22", "cam15", "cam25"
)), .Names = c("id", "camera"), class = "data.frame", row.names = c(NA,
-29L))