【问题标题】:Rename columns in a dataframe by list of correspondence通过对应列表重命名数据框中的列
【发布时间】:2021-07-08 14:12:03
【问题描述】:

我尝试通过对应表重命名数据框的列,一个两列数据框(之前的名称和之后的名称)。

在我的情况下,要重命名的数据框可能包含未在我的对应表中引用的列(这不是问题,它们不会被重命名),相反,对应表可能包含未包含在我的数据框。此列表可用于其他数据帧。

而且,至少,两个数据帧中名称的顺序可能不一样,这太容易了。

我有一个带循环的解决方案,但尝试找到另一个没有循环的解决方案。

有什么想法吗?提前致谢。

这是我的代码


df_to_rename <- data.frame(annee = 2010:2012,
                            code_commune = 67000:67002,
                            duree_occup_ou_vacance_tranche = 1:3,
                            nb_total_logements = 100:102,
                            secret_not_in_list  = 1:3)

liste_noms_col <- data.frame(noms_origine = c("mode_occ",
                                              "code_commune",
                                              "annee",
                                              "duree_occup_ou_vacance_tranche",
                                              "nb_total_logements"),
                             noms_nouveaux = c("mode d\'occupation - not included",
                                               "code INSEE commune",
                                               "Année",
                                               "durée d\'occupation (ou vacance)",
                                               "Nombre de logements"))


##### Rename by loop

l_noms <- df_to_rename %>% names()

# (i_nom  <-  l_noms[1])
for(i_nom in l_noms) {
  nom_a_changer <-
    liste_noms_col[liste_noms_col$noms_origine == i_nom, "noms_nouveaux"]
  # message(i_nom," -> ",nom_a_changer)
  
  if(length(nom_a_changer)>0) {
    df_to_rename <- df_to_rename %>%
      rename({{nom_a_changer}} := {{i_nom}})
    message(i_nom," -> ",nom_a_changer)
  }
}
df_to_rename %>% names()

【问题讨论】:

    标签: r dataframe rename


    【解决方案1】:

    使用match 并获取liste_noms_col 中存在的数据框的新列名。对于liste_noms_col中不存在的列名,保持原来的列不变。

    cols <- liste_noms_col$noms_nouveaux[match(names(df_to_rename), liste_noms_col$noms_origine)]
    cols[is.na(cols)] <- names(df_to_rename)[is.na(cols)]
    names(df_to_rename) <- cols
    df_to_rename
    
    #  Année code INSEE commune durée d'occupation (ou vacance) Nombre de logements secret_not_in_list
    #1  2010              67000                               1                 100                  1
    #2  2011              67001                               2                 101                  2
    #3  2012              67002                               3                 102                  3
    

    使用dplyr

    library(dplyr)
    
    df_to_rename %>%
      rename_with(~coalesce(liste_noms_col$noms_nouveaux[match(., liste_noms_col$noms_origine)], .))
    

    【讨论】:

      【解决方案2】:

      liste_noms_col 变成一个包含deframe、过滤感兴趣名称和rename() 的列表:

      library(dplyr)
      
      liste_noms_col <- deframe(liste_noms_col[, 2:1])
      liste_noms_col <- liste_noms_col[liste_noms_col %in% names(df_to_rename)]
      
      rename(df_to_rename, !!! liste_noms_col)
      

      审核三重爆炸 (!!!) “unquote-splice” 替换:

      expr(
        rename(df_to_rename, !!! liste_noms_col)
      )
      
      #> rename(df_to_rename, `code INSEE commune` = "code_commune", 
      #>     Année = "annee", `durée d'occupation (ou vacance)` = "duree_occup_ou_vacance_tranche", 
      #>     `Nombre de logements` = "nb_total_logements")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-28
        • 1970-01-01
        • 1970-01-01
        • 2018-12-26
        • 2021-06-05
        • 2020-08-30
        • 2014-10-26
        • 2020-01-18
        相关资源
        最近更新 更多