简体   繁体   English

使用 map function 更改嵌套 df 中的列名

[英]Change column names in nested df using map function

I am working on a project where I created a function to edit column names of a given df:我正在开发一个项目,在该项目中我创建了一个 function 来编辑给定 df 的列名:

fix_names <- function(a, b, c) {
  if (is.data.frame(a) == TRUE & is.character(b) == TRUE & is.character(c) == TRUE) {
    str_replace_all(colnames(a), pattern = b, replacement = c)
  } else {
    return("invalid inputs")
  }
}

And then I have a column, data, that contains four data frames.然后我有一列数据,它包含四个数据框。 I am trying to rename the columns of all the data frames in data using my function above inside of a map function.我正在尝试使用上面的 map function 内部的 function 重命名数据中所有数据帧的列。 It's successful in fixing the names, but I cannot figure out how to apply it to the df since the output is a list and the data frames are nested.它成功地修复了名称,但我不知道如何将其应用于 df,因为 output 是一个列表并且数据框是嵌套的。 Here's what I have:这是我所拥有的:

map(.x = df$data, ~fix_names(., "OldName", "NewName"))

Thank you!谢谢!

Edit: adding example df using mtcars编辑:使用 mtcars 添加示例 df

data(mtcars)
mtcars %>%
  group_by(cyl) %>%
  nest() -> nestMtcars

map(.x = nestMtcars$data, ~fix_names(., "mpg", "MPG"))

You could transpose the nested list to run the map function, and transpose it back to its original form:您可以transpose嵌套列表以运行map function,并将其转置回其原始形式:

library(stringr)
library(purrr)

fix_names <- function(a, b, c) {
  if (is.data.frame(a) == TRUE & is.character(b) == TRUE & is.character(c) == TRUE) {
    colnames(a) <- str_replace_all(colnames(a), pattern = b, replacement = c)
    a
  } else {
    return("invalid inputs")
  }
}

nestMtcars %>% transpose %>%
               map(~{.x$data <- fix_names(.x$data,"mpg","MPG"); .x}) %>% 
               transpose

$cyl
$cyl[[1]]
[1] 6

$cyl[[2]]
[1] 4

$cyl[[3]]
[1] 8


$data
$data[[1]]
# A tibble: 7 x 10
    MPG  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  21    160    110  3.9   2.62  16.5     0     1     4     4
2  21    160    110  3.9   2.88  17.0     0     1     4     4
3  21.4  258    110  3.08  3.22  19.4     1     0     3     1
4  18.1  225    105  2.76  3.46  20.2     1     0     3     1
5  19.2  168.   123  3.92  3.44  18.3     1     0     4     4
6  17.8  168.   123  3.92  3.44  18.9     1     0     4     4
7  19.7  145    175  3.62  2.77  15.5     0     1     5     6

$data[[2]]
# A tibble: 11 x 10
     MPG  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  22.8 108      93  3.85  2.32  18.6     1     1     4     1
 2  24.4 147.     62  3.69  3.19  20       1     0     4     2
 3  22.8 141.     95  3.92  3.15  22.9     1     0     4     2
 4  32.4  78.7    66  4.08  2.2   19.5     1     1     4     1
 5  30.4  75.7    52  4.93  1.62  18.5     1     1     4     2
 6  33.9  71.1    65  4.22  1.84  19.9     1     1     4     1
 7  21.5 120.     97  3.7   2.46  20.0     1     0     3     1
 8  27.3  79      66  4.08  1.94  18.9     1     1     4     1
 9  26   120.     91  4.43  2.14  16.7     0     1     5     2
10  30.4  95.1   113  3.77  1.51  16.9     1     1     5     2
11  21.4 121     109  4.11  2.78  18.6     1     1     4     2

$data[[3]]
# A tibble: 14 x 10
     MPG  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  18.7  360    175  3.15  3.44  17.0     0     0     3     2
 2  14.3  360    245  3.21  3.57  15.8     0     0     3     4
 3  16.4  276.   180  3.07  4.07  17.4     0     0     3     3
 4  17.3  276.   180  3.07  3.73  17.6     0     0     3     3
 5  15.2  276.   180  3.07  3.78  18       0     0     3     3
 6  10.4  472    205  2.93  5.25  18.0     0     0     3     4
 7  10.4  460    215  3     5.42  17.8     0     0     3     4
 8  14.7  440    230  3.23  5.34  17.4     0     0     3     4
 9  15.5  318    150  2.76  3.52  16.9     0     0     3     2
10  15.2  304    150  3.15  3.44  17.3     0     0     3     2
11  13.3  350    245  3.73  3.84  15.4     0     0     3     4
12  19.2  400    175  3.08  3.84  17.0     0     0     3     2
13  15.8  351    264  4.22  3.17  14.5     0     1     5     4
14  15    301    335  3.54  3.57  14.6     0     1     5     8

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

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