【问题标题】:Manipulating a character vector by considering a grouping sequnce in r (4)通过考虑 r (4) 中的分组序列来操作字符向量
【发布时间】: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;"

【问题讨论】:

    标签: r character


    【解决方案1】:

    这是一个想法。我将您的 item.map 数据集重新整形为长格式。因此item.map 与旧数据集df 具有相同的结构,但附加列used 具有所需的0 和1。

    在下一步中,我在循环中添加了一个if-函数,因此vec 中只会包含带 1 的行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2021-02-15
      相关资源
      最近更新 更多