简体   繁体   English

使用purrr重命名多个数据帧列

[英]Rename multiple dataframe columns using purrr

I have the following list of data frames containing a column named cyl 我有以下数据框列表,其中包含名为cyl的列

# Create 3 dataframes with identical column names
mt_list <- list(head(mtcars[, 1:2]), tail(mtcars[, 1:2]), mtcars[13:18, 1:2])
mt_list
#> [[1]]
#>                    mpg cyl
#> Mazda RX4         21.0   6
#> Mazda RX4 Wag     21.0   6
#> Datsun 710        22.8   4
#> Hornet 4 Drive    21.4   6
#> Hornet Sportabout 18.7   8
#> Valiant           18.1   6
#> 
#> [[2]]
#>                 mpg cyl
#> Porsche 914-2  26.0   4
#> Lotus Europa   30.4   4
#> Ford Pantera L 15.8   8
#> Ferrari Dino   19.7   6
#> Maserati Bora  15.0   8
#> Volvo 142E     21.4   4
#> 
#> [[3]]
#>                      mpg cyl
#> Merc 450SL          17.3   8
#> Merc 450SLC         15.2   8
#> Cadillac Fleetwood  10.4   8
#> Lincoln Continental 10.4   8
#> Chrysler Imperial   14.7   8
#> Fiat 128            32.4   4

# New 'cyl' column names to change to (they are a character vector)
new_cyl_names <- c("cyl1", "cyl2", "cyl3")
new_cyl_names
#> [1] "cyl1" "cyl2" "cyl3"

I would like to name cyl to be the corresponding value in the character vector new_cyl_names . 我想将cyl命名为字符向量new_cyl_names的对应值。

I tried to do this as follows: 我尝试按如下方式执行此操作:

# Custom function to change cyl to the 
# character value contained in new_colname
change_colname_cyl <- function(df, new_colname){
    df %>% 
        dplyr::rename(new_colname = cyl)
}

# The following should change the names to cyl1, cyl2, cyl3
purrr::map2(.x = mt_list, .y = new_cyl_names, ~ change_colname_cyl(.x, .y))

This results in (first data frame shown only): 这导致(仅显示第一个数据帧):

[[1]]
                   mpg new_colname
Mazda RX4         21.0           6
Mazda RX4 Wag     21.0           6
Datsun 710        22.8           4
Hornet 4 Drive    21.4           6
Hornet Sportabout 18.7           8
Valiant           18.1           6

Could anyone please help me use purrr correctly for this ie change cyl to cyl1 in this case instead of new_colname per above? 有没有人可以帮我正确使用purrr ,即在这种情况下将cyl更改为cyl1而不是每个上面的new_colname

I made a slight modification of your function. 我对你的功能稍作修改。 I think now it works. 我认为现在它有效。 See this ( https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html ) to learn more about standard evaluation and non-standard evaluation in dplyr . 请参阅此( https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html )以了解有关dplyr标准评估和非标准评估的dplyr

library(tidyverse)

# List of data frames
mt_list <- list(head(mtcars[, 1:2]), tail(mtcars[, 1:2]), mtcars[13:18, 1:2])

# New column names
new_cyl_names <- c("cyl1", "cyl2", "cyl3")

# Create the function
change_colname_cyl <- function(df, new_colname){
  df %>% rename(!!new_colname := cyl)
}

# Apply the function
map2(mt_list, new_cyl_names, ~ change_colname_cyl(.x, .y))
[[1]]
                   mpg cyl1
Mazda RX4         21.0    6
Mazda RX4 Wag     21.0    6
Datsun 710        22.8    4
Hornet 4 Drive    21.4    6
Hornet Sportabout 18.7    8
Valiant           18.1    6

[[2]]
                mpg cyl2
Porsche 914-2  26.0    4
Lotus Europa   30.4    4
Ford Pantera L 15.8    8
Ferrari Dino   19.7    6
Maserati Bora  15.0    8
Volvo 142E     21.4    4

[[3]]
                     mpg cyl3
Merc 450SL          17.3    8
Merc 450SLC         15.2    8
Cadillac Fleetwood  10.4    8
Lincoln Continental 10.4    8
Chrysler Imperial   14.7    8
Fiat 128            32.4    4

Update 更新

Based on Paul's comments. 根据保罗的评论。 The following seems to be a more direct and concise way to rename the column. 以下似乎是重命名列的更直接和简洁的方法。

map2(mt_list, new_cyl_names, ~rename(.x, !!.y := cyl))

We could use the setnames from data.table 我们可以使用setnamesdata.table

library(data.table)
library(tidyverse)
map2(mt_list, new_cyl_names, ~setnames(.x, 'cyl', .y))
mt_list
#[[1]]
#                   mpg cyl1
#Mazda RX4         21.0    6
#Mazda RX4 Wag     21.0    6
#Datsun 710        22.8    4
#Hornet 4 Drive    21.4    6
#Hornet Sportabout 18.7    8
#Valiant           18.1    6

#[[2]]
#                mpg cyl2
#Porsche 914-2  26.0    4
#Lotus Europa   30.4    4
#Ford Pantera L 15.8    8
#Ferrari Dino   19.7    6
#Maserati Bora  15.0    8
#Volvo 142E     21.4    4

#[[3]]
#                     mpg cyl3
#Merc 450SL          17.3    8
#Merc 450SLC         15.2    8
#Cadillac Fleetwood  10.4    8
#Lincoln Continental 10.4    8
#Chrysler Imperial   14.7    8
#Fiat 128            32.4    4

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

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