简体   繁体   English

通过使用包含列名的向量重命名列表中的列表条目

[英]Renaming entries of a list within a list, by using a vector containing the column names

My data looks as follows:我的数据如下所示:

DT1 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000, 
2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 
2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001, 
2001, 2002, 2002, 2002), Municipality = c("Something", "Anything", 
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything", 
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything", 
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything", 
"Nothing", "Something", "Anything", "Nothing", "Something", "Anything", 
"Nothing"), Values = c(0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 
0.85, 0.99, 0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99, 
0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99)), row.names = c(NA, 
-27L), class = c("tbl_df", "tbl", "data.frame"))
DT1_list <- DT1%>%
  group_split(Province, Year)

I want to rename all the columns, using a vector as follows:我想使用如下向量重命名所有列:

colnames <- c("newname1","newname2","newname3","newname4")

for (i in DT1_list) {
    names(DT1_list)[[i]] <- colnames
    }

The problem is that names(DT1_list)[[i]] does not give column names but NULL .问题是names(DT1_list)[[i]]没有给出列名,而是NULL

What is the correct way to do this?这样做的正确方法是什么?

EDIT:编辑:

I noticed that my question, was not a good enough representation of my actual problem (my apologies, I did not foresee the the issue).我注意到我的问题并不能很好地代表我的实际问题(我很抱歉,我没有预见到这个问题)。 My actual problem is that I want to rename 3 out of 4 columns:我的实际问题是我想重命名 4 列中的 3 列:

colnames <- c("newname1","newname2","newname3")

If I use the answers provided, the fourth column becomes NA .如果我使用提供的答案,第四列将变为NA Is there anyway to keep the other columns intact?无论如何要保持其他列完好无损?

You could use purrr:map :您可以使用purrr:map

library(purrr)                                                                                                                                                              0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99)), row.names = c(NA, 
                                                                                                                                                                                                                                   -27L), class = c("tbl_df", "tbl", "data.frame"))
DT1_list <- DT1%>%
  group_split(Province, Year)

library(purrr)                                                                                                                                                              0.59, 0.58, 0.66, 0.53, 0.94, 0.2, 0.86, 0.85, 0.99)), row.names = c(NA, 
                                                                                                                                                                                                                                   -27L), class = c("tbl_df", "tbl", "data.frame"))
DT1_list <- DT1%>%
  group_split(Province, Year)

DT1_list %>% map(~{colnames <- colnames(.x)
                   #You could also use str_replace
                   #colnames <- stringr::str_replace(colnames,"Values","NewValues")
                   colnames[1:3] <- c("newname1","newname2","newname3")
                   colnames(.x)<- colnames
                   .x})

[[1]]
# A tibble: 3 x 4
  newname1 newname2 newname3  Values
     <dbl>    <dbl> <chr>      <dbl>
1        1     2000 Something  0.59 
2        1     2000 Anything   0.580
3        1     2000 Nothing    0.66 

[[2]]
# A tibble: 3 x 4
  newname1 newname2 newname3  Values
     <dbl>    <dbl> <chr>      <dbl>
1        1     2001 Something   0.53
2        1     2001 Anything    0.94
3        1     2001 Nothing     0.2 

...

You can use lapply / map :您可以使用lapply / map

lapply(DT1_list, setNames, colnames)

#[[1]]
# A tibble: 3 x 4
#  newname1 newname2 newname3  newname4
#     <dbl>    <dbl> <chr>        <dbl>
#1        1     2000 Something    0.59 
#2        1     2000 Anything     0.580
#3        1     2000 Nothing      0.66 

#[[2]]
# A tibble: 3 x 4
#  newname1 newname2 newname3  newname4
#     <dbl>    <dbl> <chr>        <dbl>
#1        1     2001 Something     0.53
#2        1     2001 Anything      0.94
#3        1     2001 Nothing       0.2 
#...
#...

When you want to rename less columns than your original data use:当您要重命名的列少于原始数据时,请使用:

inds <- seq_along(colnames)
lapply(DT1_list, function(x) {names(x)[inds] <- colnames;x})

Or:或者:

library(dplyr)
library(purrr)

map(DT1_list, ~.x %>% rename_with(~colnames, inds))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM