【问题标题】:Renaming column names in R using numbers in the pre-existing column names as indices and adding text使用预先存在的列名中的数字作为索引重命名 R 中的列名并添加文本
【发布时间】:2020-11-20 22:30:37
【问题描述】:

我将数据从调查软件导出到 R 中,列名的格式为 Qx_y,其中 x 是部分,y em> 是该部分中的问题编号。我想以 XY 的形式重命名列,其中 X 是每个部分对应的名称 (1=A, 2=B...) 和 Y 是与每个问题编号对应的名称(例如 topleft = 1、topright = 2 等)。因此,Q1_1 将变为 A.topleftQ1_2 将变为 A.topright

从搜索线程看来,我可能需要 paste0 和 regex 命令的组合。这是一些示例数据和两个名称列表:

Q1_1 = c(1:3)
Q1_2 = c(1:3)
Q1_3 = c(1:3)
Q1_4 = c(1:3)
Q2_1 = c(1:3)
Q2_2 = c(1:3)
Q2_3 = c(1:3)
Q2_4 = c(1:3)
Q3_1 = c(1:3)
Q3_2 = c(1:3)
Q3_3 = c(1:3)
Q3_4 = c(1:3)
df <- data.frame(Q1_1,Q1_2,Q1_3,Q1_4,Q2_1,Q2_2,Q2_3,Q2_4,Q3_1,Q3_2,Q3_3,Q3_4)
sections = ("A","B","C")
questions = ("topleft","topright","bottomleft","bottomright")

(可能有一种更简单的方法来指定这些数据,但我是 R 新手。)如何重命名我的所有列?

【问题讨论】:

    标签: r loops dplyr rename


    【解决方案1】:

    这是另一种方法。我认为,这可能不是一种更容易,而是一种更大众化和透明的方式。 我不确定这是否是一个好的解决方案。但是当我自己质疑这种方法时,我已经在编写代码了......所以我们在这里:D

    # some example data
    test_data <- dplyr::tibble(Q1_1=1:3,
                               Q1_2=1:3,
                               Q2_1=1:3,
                               Q2_4=1:3,
                               Q3_3=1:3)
    
    # A tibble: 3 x 5
       Q1_1  Q1_2  Q2_1  Q2_4  Q3_3
      <int> <int> <int> <int> <int>
    1     1     1     1     1     1
    2     2     2     2     2     2
    3     3     3     3     3     3
    
    # creating named vectors as 'lookup tables'
    # values: new values
    # names: old values
    get_section <- c("A","B","C")
    names(get_section) <- c("1","2","3")
    get_question <- c("topleft","topright","bottomleft","bottomright")
    names(get_question) <- c("1","2","3","4")
    
    # split and recode
    section_label <- colnames(test_data) %>%
      stringr::str_match("(?:Q)(.*)(?:_.*)") %>%
      .[,2] %>%
      get_section[.]
    question_label <- colnames(test_data) %>%
      stringr::str_match("(?:Q.*_)(.*)") %>%
      .[,2] %>%
      get_question[.]
    
    # rename cols
    colnames(test_data) <- paste(section_label,question_label,sep=".")
    
    # A tibble: 3 x 5
      A.topleft A.topright B.topleft B.bottomright C.bottomleft
          <int>      <int>     <int>         <int>        <int>
    1         1          1         1             1            1
    2         2          2         2             2            2
    3         3          3         3             3            3
    

    【讨论】:

      【解决方案2】:

      您可以使用outer 创建列名组合:

      names(df) <- t(outer(sections, questions, paste, sep = "_"))
      

      【讨论】:

      • 这个解决方案可以简化吗colnames(df) &lt;- c(paste0(sections[[1]], "_", questions), paste0(sections[[2]], "_", questions),paste0(sections[[3]], "_", questions))
      • 是的,您可以使用sapply 而非sectionssapply(sections, function(x) paste0(x, "_", questions))
      猜你喜欢
      • 2020-05-28
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      相关资源
      最近更新 更多