Reputation: 4201
Using purrr
, I've summarized iris
data into a new mutated list-column:
library(tidyverse)
my_tibble <-
iris %>%
nest(data = everything()) %>%
mutate(summary_tbl = map(.x = data,
~ .x %>%
group_by(Species) %>%
summarise(mean_by_cat = mean(Sepal.Length))))
my_tibble
#> # A tibble: 1 x 2
#> data summary_tbl
#> <list> <list>
#> 1 <tibble [150 x 5]> <tibble [3 x 2]>
Created on 2021-03-16 by the reprex package (v0.3.0)
If we unnest data
or summary_tbl
we can see that both contain a tibble, with some overlapping column names (in this case Species
):
my_tibble %>%
pull(data)
## [[1]]
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## # ... with 140 more rows
my_tibble %>%
pull(summary_tbl)
## [[1]]
## # A tibble: 3 x 2
## Species mean_by_cat
## * <fct> <dbl>
## 1 setosa 5.01
## 2 versicolor 5.94
## 3 virginica 6.59
Is there an efficient way to rename a column name that appears inside any of my_tibble
's list-columns?
For example, if we define:
var_to_rename <- "Species"
new_name <- "my_grouping_var"
Then provided with my_tibble
, var_to_rename
, and new_name
, how can we programmatically get the following?
my_tibble %>%
pull(data)
## [[1]]
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width my_grouping_var
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## # ... with 140 more rows
my_tibble %>%
pull(summary_tbl)
## [[1]]
## # A tibble: 3 x 2
## my_grouping_var mean_by_cat
## * <fct> <dbl>
## 1 setosa 5.01
## 2 versicolor 5.94
## 3 virginica 6.59
Clearly I could have renamed Species
to my_grouping_var
before the mutate
part, but my question is aimed at renaming an existing tibble after the fact.
Upvotes: 1
Views: 94
Reputation: 8523
If you want to use a function you can use rename()
with the curly-curly operator ({{}}
) and the colon-equal operator (:=
):
foo = function(var_to_rename, new_name){
my_tibble %>%
mutate(summary_tbl = map(summary_tbl, ~{
rename(.x, {{new_name}}:={{var_to_rename}})
}))
}
my_tibble2=foo("Species", "my_grouping_var")
my_tibble2 %>%
pull(summary_tbl)
#> [[1]]
#> # A tibble: 3 x 2
#> my_grouping_var mean_by_cat
#> <fct> <dbl>
#> 1 setosa 5.01
#> 2 versicolor 5.94
#> 3 virginica 6.59
Created on 2021-03-16 by the reprex package (v1.0.0)
More info on how to use dplyr
's tidy-evaluation in functions on https://dplyr.tidyverse.org/articles/programming.html.
Upvotes: 3
Reputation: 389265
You can do :
library(dplyr)
my_tibble <- my_tibble %>%
mutate(across(.fns = ~.[[1]] %>%
rename_with(~new_name, all_of(var_to_rename)) %>% list()))
my_tibble %>% pull(summary_tbl)
#[[1]]
# A tibble: 3 x 2
# my_grouping_var mean_by_cat
#* <fct> <dbl>
#1 setosa 5.01
#2 versicolor 5.94
#3 virginica 6.59
Upvotes: 2