I am trying to create a function that will mutate a column if it exists. If the column does exist, I return a data frame with two columns. I'd like help unpacking this data frame column, into its component columns:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
my_transformation = function(df){
df %>%
mutate(across(any_of('cyl'), function(x) tibble(a = x + 3, b = x + 1)))
}
df_1 = as_tibble(mtcars)
df_2 = df_1 %>% select(-cyl)
my_transformation(df_1)
#> # A tibble: 32 x 11
#> mpg cyl$a $b disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 9 7 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 9 7 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 7 5 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 9 7 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 11 9 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 9 7 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 11 9 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 7 5 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 7 5 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 9 7 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
my_transformation(df_2)
#> # A tibble: 32 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 22.8 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
Created on 2020-08-22 by the reprex package (v0.3.0)
As you can see, when calling my_transformation(df_1), there are two subcolumns: cyl$a and cyl$b. How do I get these to be regular columns?
I have tried unnest(cyl) but had no success.
I think what you're after is something like
mtcars %>% mutate(across(cyl, list(a = ~ .x + 3, b = ~ .x + 1)))
# mpg cyl disp hp drat wt qsec vs am gear carb cyl_a cyl_b
# 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 9 7
# 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 9 7
# 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 7 5
# 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 9 7
# 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 11 9
# 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 9 7
# ...
Note that the .fns argument of across can take a list of (lambda) functions; so if you replace function(x) tibble(a = ..., b = ...) with list(a = ~ ..., b = ~ ...) the new mutate (dplyr >= 1.0.0) will automatically create columns cyl_a and cyl_b.
So the only way I've found to drop the "nesting" for dataframe columns is by not supplying an LHS argument to mutate as documented here
Unfortunately, using across to check for missing columns is not possible, as it uses .names to assign something on the LHS.
Therefore, I'm taking the approach of inserting the missing column if it is missing and then calling mutate without across.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tibble)
my_transformation = function(df){
cols <- c(cyl = NA_real_)
df %>%
add_column(!!!cols[!names(cols) %in% names(.)]) %>%
mutate(tibble(a = cyl + 3, b = cyl + 1))
}
df_1 = as_tibble(mtcars)
df_2 = df_1 %>% select(-cyl)
my_transformation(df_1)
#> # A tibble: 32 x 13
#> mpg cyl disp hp drat wt qsec vs am gear carb a b
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 9 7
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 9 7
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 7 5
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 9 7
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 11 9
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 9 7
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 11 9
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 7 5
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 7 5
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 9 7
#> # … with 22 more rows
my_transformation(df_2)
#> # A tibble: 32 x 13
#> mpg disp hp drat wt qsec vs am gear carb cyl a b
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 160 110 3.9 2.62 16.5 0 1 4 4 NA NA NA
#> 2 21 160 110 3.9 2.88 17.0 0 1 4 4 NA NA NA
#> 3 22.8 108 93 3.85 2.32 18.6 1 1 4 1 NA NA NA
#> 4 21.4 258 110 3.08 3.22 19.4 1 0 3 1 NA NA NA
#> 5 18.7 360 175 3.15 3.44 17.0 0 0 3 2 NA NA NA
#> 6 18.1 225 105 2.76 3.46 20.2 1 0 3 1 NA NA NA
#> 7 14.3 360 245 3.21 3.57 15.8 0 0 3 4 NA NA NA
#> 8 24.4 147. 62 3.69 3.19 20 1 0 4 2 NA NA NA
#> 9 22.8 141. 95 3.92 3.15 22.9 1 0 4 2 NA NA NA
#> 10 19.2 168. 123 3.92 3.44 18.3 1 0 4 4 NA NA NA
#> # … with 22 more rows
Created on 2020-08-23 by the reprex package (v0.3.0)
Not a huge fan of the solution. but it does work. I'm considering creating a github issue for instances where you want to return an output data frame column, using mutate but only if an input column exists.
Related
I'm trying to use dplyr::mutate to change a dynamic column with conditions using other columns dynamically.
I've got this bit of code:
d <- mtcars %>% tibble
fld_name <- "mpg"
other_fld_name <- "cyl"
d <- d %>% mutate(!!fld_name := ifelse(!!other_fld_name < 5,NA,!!fld_name))
which sets mpg to
mpg
<chr>
1 mpg
2 mpg
3 mpg
4 mpg
5 mpg
6 mpg
7 mpg
8 mpg
9 mpg
10 mpg
it seems to select the field on the LHS of assignment operator, but just pastes the field name on the RHS.
Removing the unquotes on the RHS yields the same result.
Any help is much appreciated.
use get to retreive column value instead
library(tidyverse)
d <- mtcars %>% tibble
fld_name <- "mpg"
other_fld_name <- "cyl"
d %>% mutate(!!fld_name := ifelse(get(other_fld_name) < 5 ,NA, get(fld_name)))
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 NA 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 NA 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 NA 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
Created on 2021-06-22 by the reprex package (v2.0.0)
We can also use ensym function to quote variable name stored as string and unquote it with !! like the following:
library(rlang)
d <- mtcars %>% tibble
fld_name <- "mpg"
other_fld_name <- "cyl"
d %>%
mutate(!!ensym(fld_name) := ifelse(!!ensym(other_fld_name) < 5, NA, !!ensym(fld_name)))
# A tibble: 32 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
3 NA 4 108 93 3.85 2.32 18.6 1 1 4 1
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
8 NA 4 147. 62 3.69 3.19 20 1 0 4 2
9 NA 4 141. 95 3.92 3.15 22.9 1 0 4 2
10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
# ... with 22 more rows
We could also use .data
library(dplyr)
d %>%
mutate(!! fld_name := case_when(.data[[other_fld_name]] >=5 ~
.data[[fld_name]]))
-output
# A tibble: 32 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
3 NA 4 108 93 3.85 2.32 18.6 1 1 4 1
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
8 NA 4 147. 62 3.69 3.19 20 1 0 4 2
9 NA 4 141. 95 3.92 3.15 22.9 1 0 4 2
10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
# … with 22 more rows
data
d <- mtcars %>%
as_tibble
fld_name <- "mpg"
other_fld_name <- "cyl"
I would like to know how I can transpose three columns that have been placed at the end of my table, moving them to the beginning, also deleting the first column that is not needed.
The dplyr package from the tidyverse can help you with this. Use select to keep/remove columns, and relocate to move them. Have a look at the documentation https://dplyr.tidyverse.org/ for more info:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
mtcars <- as_tibble(mtcars)
mtcars
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
mtcars %>% select(-mpg) %>% relocate(c(am, gear, carb))
#> # A tibble: 32 x 10
#> am gear carb cyl disp hp drat wt qsec vs
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 4 4 6 160 110 3.9 2.62 16.5 0
#> 2 1 4 4 6 160 110 3.9 2.88 17.0 0
#> 3 1 4 1 4 108 93 3.85 2.32 18.6 1
#> 4 0 3 1 6 258 110 3.08 3.22 19.4 1
#> 5 0 3 2 8 360 175 3.15 3.44 17.0 0
#> 6 0 3 1 6 225 105 2.76 3.46 20.2 1
#> 7 0 3 4 8 360 245 3.21 3.57 15.8 0
#> 8 0 4 2 4 147. 62 3.69 3.19 20 1
#> 9 0 4 2 4 141. 95 3.92 3.15 22.9 1
#> 10 0 4 4 6 168. 123 3.92 3.44 18.3 1
#> # ... with 22 more rows
Created on 2021-04-22 by the reprex package (v1.0.0)
Before, I used group_by_at to group by a vector of strings or by NULL:
library(tidyverse)
grouping_1 <- c("cyl", "vs")
grouping_2 <- NULL
mtcars %>% group_by_at(grouping_1)
mtcars %>% group_by_at(grouping_2)
The help of group_by_at indicates that the function is superseded and that across should be used instead. But, grouping by NULL gives an error
mtcars %>% group_by(across(grouping_1)) # this works
mtcars %>% group_by(across(grouping_2)) # this gives an error
For me, group_by_at used in the way described has been useful because in my functions I can use the same code without checking every time whether the grouping argument is empty (NULL) or not.
It is still ok to use syms to splice strings into group_by using !!!.
library(tidyverse)
grouping_1 <- c("cyl", "vs")
grouping_2 <- NULL
sym_gr_1 <- syms(grouping_1)
sym_gr_2 <- syms(grouping_2)
mtcars %>% group_by(!!! sym_gr_1) # this works
#> # A tibble: 32 x 11
#> # Groups: cyl, vs [5]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
mtcars %>% group_by(!!! sym_gr_2) # this works
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
Created on 2020-06-20 by the reprex package (v0.3.0)
Using dplyr::across() another option (on top of the official way to do with all_of as posted in the answer below) is to wrap the strings containing the variable names in c(). This even works, when the object is NULL. However, results in a note, reminding use to better use all_of.
grouping_1 <- c("cyl", "vs")
grouping_2 <- NULL
mtcars %>% group_by(across(c(grouping_1)))
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(grouping_1)` instead of `grouping_1` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> # A tibble: 32 x 11
#> # Groups: cyl, vs [5]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
mtcars %>% group_by(across(c(grouping_2)))
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(grouping_2)` instead of `grouping_2` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
Created on 2021-05-30 by the reprex package (v0.3.0)
Using all_of:
library(tidyverse)
mtcars %>% group_by(across(all_of(grouping_1))) # this works
mtcars %>% group_by(across(all_of(grouping_2))) # this works
I am trying to write a custom function that uses rlang's non-standard evaluation to group a dataframe by more than one variable.
This is what I've-
library(rlang)
# function definition
tryfn <- function(data, groups, ...) {
# preparing data
df <- dplyr::group_by(data, !!!rlang::enquos(groups))
print(head(df))
# applying some function `.f` on df that absorbs `...`
# .f(df, ...)
}
This works with a single grouping variable-
# works
tryfn(mtcars, am)
#> # A tibble: 6 x 11
#> # Groups: am [2]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
But if try to use more than one grouping variable, this doesn't work-
# doesn't work
tryfn(mtcars, c(am, cyl))
#> Error: Column `c(am, cyl)` must be length 32 (the number of rows) or one, not 64
# doesn't work
tryfn(mtcars, list(am, cyl))
#> Error: Column `list(am, cyl)` must be length 32 (the number of rows) or one, not 2
We could parse as an expression with enexpr and use !!!
tryfn <- function(data, groups, ...) {
groups <- as.list(rlang::enexpr(groups))
groups <- if(length(groups) > 1) groups[-1] else groups
group_by(data, !!!groups)
}
-testing
tryfn(mtcars, am)
# A tibble: 32 x 11
# Groups: am [2]
# mpg cyl disp hp drat wt qsec vs am gear carb
# * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
# 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
# 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
# 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
# 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
# 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
# 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
# 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
# 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
# … with 22 more rows
tryfn(mtcars, c(am, cyl))
# A tibble: 32 x 11
# Groups: am, cyl [6]
# mpg cyl disp hp drat wt qsec vs am gear carb
# * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
# 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
# 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
# 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
# 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
# 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
# 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
# 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
# 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
# … with 22 more rows
I have a number of dataframes and a series of changes I want to make to each of them. For this example, let to the desired change be simply making each data frame a tibble using as_tibble. I know there are various ways of doing this, but I'd like to do this using purrr:walk.
For data frames df1 and df2,
df1 <- mtcars
df2 <- mtcars
I'd like to do the equivalent of
df1 %<>% as_tibble
df2 %<>% as_tibble
using walk. My attempt:
library(tidyverse)
walk(c(df1, df2), ~ assign(deparse(substitute(.)), as_tibble(.)))
This runs but does not make the desired change:
is_tibble(df1)
#> [1] FALSE
Here is how you can combine assign with walk (see the comments the code for more explanation)-
library(tidyverse)
# data
df1 <- mtcars
df2 <- mtcars
# creating tibbles
# this creates a list of objects with names ("df1", "df2")
tibble::lst(df1, df2) %>%
purrr::walk2(
.x = names(.), # names to assign
.y = ., # object to be assigned
.f = ~ assign(x = .x,
value = tibble::as.tibble(.y),
envir = .GlobalEnv)
)
# checking the newly created tibbles
df1
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
df2
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
Created on 2018-11-13 by the reprex package (v0.2.1)