通过考虑 r (4) 中的分组序列来操纵字符向量

Manipulating a character vector by considering a grouping sequnce in r (4)

我正在尝试编写基于组变量的代码,item.map 具有项目信息,其中包括一个 q 矩阵,显示哪个项目与哪个组相关联。

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;"

这是一个想法。我将您的 item.map 数据集重塑为长格式。因此 item.map 得到了与旧数据集 df 相同的结构,但增加了包含所需 0 和 1 的列 used

在下一步中,我在循环中添加了一个 if 函数,因此只有包含 1 的行将包含在 vec.

library(stringr)

# original dataset item.map
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)

# reshape item.map 
item.map2 <- item.map %>%
  pivot_longer(-item.id, 
               names_to = "group",
               values_to = "used") %>%
  arrange(group) %>%
  mutate(group = str_replace(group, "group.", "G"),
         item.id = as.character(item.id))

# empty vector
vec <- NULL
for(i in 1:nrow(item.map2)) {
  if(item.map2[i, "used"] == 1) {
  res <- which(str_extract(item.map2[i, "item.id"], "[0-9]{2,}") == factor)
  
  text <- paste("(", item.map2[i, "group"], ", ", item.map2[i, "item.id"],
                ", fixed[", c(0:length(factor)) ,"]) = ", 
                ifelse(res == 0:length(factor) | 0 == 0:length(factor), 
                       "1.0", "0.0"),";", sep = "")
  
  vec <- c(vec, text)
  }
}

vec

输出

[1] "(G1, 54_a, fixed[0]) = 1.0;" "(G1, 54_a, fixed[1]) = 1.0;" "(G1, 54_a, fixed[2]) = 0.0;"
 [4] "(G1, 54_b, fixed[0]) = 1.0;" "(G1, 54_b, fixed[1]) = 1.0;" "(G1, 54_b, fixed[2]) = 0.0;"
 [7] "(G1, 44_a, fixed[0]) = 1.0;" "(G1, 44_a, fixed[1]) = 0.0;" "(G1, 44_a, fixed[2]) = 1.0;"
[10] "(G2, 54_b, fixed[0]) = 1.0;" "(G2, 54_b, fixed[1]) = 1.0;" "(G2, 54_b, fixed[2]) = 0.0;"
[13] "(G2, 44_c, fixed[0]) = 1.0;" "(G2, 44_c, fixed[1]) = 0.0;" "(G2, 44_c, fixed[2]) = 1.0;"