【发布时间】:2020-09-23 19:37:03
【问题描述】:
我正在尝试基于 Group 变量 item.map 编写代码,该变量具有包含 q 矩阵的项目信息,该 q-matrix 显示哪个项目与哪个组相关联。
group <- c(1,2)
ids <- c("54_a","54_b","44_a","44_c")
item.map <- data.frame(
item.id = c("54_a","54_b","44_a","44_c"),
group.1 = c(1,1,1,0),
group.2 = c(0,1,0,1))
factor <- c(54,44)
在这个 item.map 中 group.1 有 3 个项目,而 group.2 有两个项目。使用这个 item.map 我想在下面的代码块中分配这些项目,但我无法插入 item.map 信息。
library(stringr)
# define df for all ids and group combinations
group_g <- paste("G", 1:length(group), sep ="")
df <- data.frame(ids, group = rep(group_g, each = length(ids)))
# empty vector
vec <- NULL
for(i in 1:nrow(df)) {
res <- which(str_extract(df[i, "ids"], "[0-9]{2,}") == factor)
text <- paste("(", df[i, "group"], ", ", df[i, "ids"], ", fixed[", c(0:length(factor)) ,"]) = ", ifelse(res == 0:length(factor) | 0 == 0:length(factor), "1.0", "0.0"),";", sep = "")
vec <- c(vec, text)
}
> vec
"(G1, 54_a, fixed[0]) = 1.0;" "(G1, 54_a, fixed[1]) = 1.0;" "(G1, 54_a, fixed[2]) = 0.0;"
"(G1, 54_b, fixed[0]) = 1.0;" "(G1, 54_b, fixed[1]) = 1.0;" "(G1, 54_b, fixed[2]) = 0.0;"
"(G1, 44_a, fixed[0]) = 1.0;" "(G1, 44_a, fixed[1]) = 0.0;" "(G1, 44_a, fixed[2]) = 1.0;"
"(G1, 44_c, fixed[0]) = 1.0;" "(G1, 44_c, fixed[1]) = 0.0;" "(G1, 44_c, fixed[2]) = 1.0;"
"(G2, 54_a, fixed[0]) = 1.0;" "(G2, 54_a, fixed[1]) = 1.0;" "(G2, 54_a, fixed[2]) = 0.0;"
"(G2, 54_b, fixed[0]) = 1.0;" "(G2, 54_b, fixed[1]) = 1.0;" "(G2, 54_b, fixed[2]) = 0.0;"
"(G2, 44_a, fixed[0]) = 1.0;" "(G2, 44_a, fixed[1]) = 0.0;" "(G2, 44_a, fixed[2]) = 1.0;"
"(G2, 44_c, fixed[0]) = 1.0;" "(G2, 44_c, fixed[1]) = 0.0;" "(G2, 44_c, fixed[2]) = 1.0;"
因此,基于所需输出中的item.map,G1 不应该有项目 44_c,G2 不应该有项目 54_a 和 44_a
想要的输出是:
> vec
"(G1, 54_a, fixed[0]) = 1.0;" "(G1, 54_a, fixed[1]) = 1.0;" "(G1, 54_a, fixed[2]) = 0.0;"
"(G1, 54_b, fixed[0]) = 1.0;" "(G1, 54_b, fixed[1]) = 1.0;" "(G1, 54_b, fixed[2]) = 0.0;"
"(G1, 44_a, fixed[0]) = 1.0;" "(G1, 44_a, fixed[1]) = 0.0;" "(G1, 44_a, fixed[2]) = 1.0;"
"(G2, 54_b, fixed[0]) = 1.0;" "(G2, 54_b, fixed[1]) = 1.0;" "(G2, 54_b, fixed[2]) = 0.0;"
"(G2, 44_c, fixed[0]) = 1.0;" "(G2, 44_c, fixed[1]) = 0.0;" "(G2, 44_c, fixed[2]) = 1.0;"
【问题讨论】: