观察之间的重叠百分比

时间:2014-11-07 04:42:52

标签: r matrix

偏好卡可识别特定外科手术的资源和偏好,包括手术材料和设备。每位外科医生都有自己的手术偏好卡。我有兴趣检查从一位外科医生到下一位外科医生的项目重叠(通过热图)。

示例:

Physician   Item 1  Item 2  Item 3
1            Yes    Yes      No
2            Yes    Yes      No
3            No     No       Yes
4            No     No       Yes

医师1& 2使用的项目有100%重叠(例如,所有使用的项目都匹配) 医师1& 3使用的项目有0%重叠(例如,没有使用的项目匹配) 医师1& 4使用的项目有0%重叠 医师2& 3使用的项目有0%重叠 医师2& 4使用的项目有0%重叠 医师3& 4使用的项目有33.3%的重叠(例如3项中的1项匹配)

在表格中,这看起来像

Physician   1       2      3      4
1          100%         
2          100%   100%      
3            0%     0%    100%  
4            0%     0%     33%   100%

1 个答案:

答案 0 :(得分:0)

假设33%重叠是一个错字,你可以这样做:

r1 <- combn(df$Physician, 2, FUN=function(x) {
                     x1 <- match(x[1], df$Physician)
                     x2 <- match(x[2], df$Physician)
         c(paste0(100*sum(df[x1,-1]==df[x2,-1])/ncol(df[,-1]), "%"), 
                                         paste(x1, x2, sep=" "))})


indx <- as.matrix(read.table(text=r1[2,]))
res <- matrix('', ncol=nrow(df), nrow=nrow(df),
             dimnames=list(df$Physician, df$Physician))

res[dim(res)[1]+1 - indx] <- r1[1,]
diag(res) <- '100%'
as.data.frame(res)
#    1    2    3    4
#1 100%               
#2 100% 100%          
#3   0%   0% 100%     
#4   0%   0% 100% 100%

数据

df <-  structure(list(Physician = 1:4, Item.1 = c("Yes", "Yes", "No", 
"No"), Item.2 = c("Yes", "Yes", "No", "No"), Item.3 = c("No", 
"No", "Yes", "Yes")), .Names = c("Physician", "Item.1", "Item.2", 
"Item.3"), class = "data.frame", row.names = c(NA, -4L))
相关问题