我想从我的数据框中的一个变量创建一个矩阵。矩阵的每个单元格将如下填充:
sum(Rin1,Rin2)/sum(Rin),...,sum(Rin1,Rin28)/sum(Rin)
sum(Rin2,Rin3)/sum(Rin),...,sum(Rin2,Rin28)/sum(Rin)
我使用这个脚本,但它对我不起作用:
# Keep one column that has proportion of Rin=Tdin/User.in
df.in <- df[,c(5)]
# Create matrix for rows' combination
a<-t(combn(28,2))
# Create empty matrix according length of rows
output <- matrix ('',28,28)
# Fill matrix with sum of rows divide sum of Rin
for (i in 1:nrow(a)) {
# I came up with this line from my previous work!
output[cbind(a[i,][1],a[i,][2])] <-unlist(intersect(df.in[a[i,]][1],df.in[a[i,]][2]))
}
提前感谢任何建议。
样本数据框:
df <- structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("Political.Issue", "Women.s.Rights", "Workers..Rights"),
class = "factor"), Kcore.in = c(0, 1, 2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 21,
22, 25,28, 30, 39, 40, 41, 42, 44, 82), Tdin = c(2090, 3217, 6110, 32427,6612, 3425, 11940,
17985, 1998, 882, 8661, 3228, 701, 656, 269,393, 7737, 339, 6456, 1950, 99, 302, 43320, 1087,
3655, 2103,562, 2135), User.in = c(4723L, 183L, 87L, 62L, 24L, 13L, 13L,25L, 7L, 2L, 9L, 9L,
2L, 2L, 1L, 1L, 13L, 2L, 3L, 3L, 1L, 1L,4L, 1L, 2L, 2L, 1L, 2L), Rin = c(0.442515350412873,
17.5792349726776,70.2298850574713, 523.016129032258, 275.5, 263.461538461538,918.461538461538,
719.4, 285.428571428571, 441, 962.333333333333,358.666666666667, 350.5, 328, 269, 393, 595.153846153846,
169.5,2152, 650, 99, 302, 10830, 1087, 1827.5, 1051.5, 562, 1067.5)), .Names = c("Subject","Kcore.in", "Tdin",
"User.in", "Rin"), row.names = c(NA, -28L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Subject",
drop = TRUE, indices = list( 0:27), group_sizes = 28L, biggest_group_size = 28L, labels = structure(list(Subject =
structure(1L, .Label = c("Political.Issue", "Women.s.Rights","Workers..Rights"), class = "factor")),
row.names = c(NA,-1L), class = "data.frame", vars = "Subject", drop = TRUE, .Names = "Subject"))
这是你想要做的吗?
out <- matrix(NA, nrow = dim(df)[1], ncol = dim(df)[1])
sum <- sum(df$Rin)
for (t1 in 1:dim(df)[1]){
for (t2 in 1:dim(df)[1]){
out[t1, t2] <- sum(df$Rin[t1], df$Rin[t2])/sum
}
}