Hello! I'm super new to RStudio and I have some troubles.
I've got a dataset and I need to change many columns format from int to factor. All these columns start with "gg_". So not to go one by one I want to use across function.
So I'm trying to do it like that:
And I've got some erros:
Problem with mutate() input ..2.
i ..2 = as.factor.
x ..2 must be a vector, not a function.
Could you help me how to fix it? I used mutate several times defore but every time with a certain column names and everything was fine. Here I'm missing something.
Thanx!
The function to apply to the selected columns is one of the arguments of the across() function. I moved your closing parenthesis to accomplish that. I also changed the function from as.factor() to factor().
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
DF <- data.frame(Name = c("A","B"), gg_1 = c(1,0), gg_2 = c(0,1),
gg_3 = c(1,1))
DF <- DF %>%
mutate(across(starts_with("gg_"),.fns = factor,
levels = c(0,1),
labels = c("’No’", "’Yes’")))
str(DF)
#> 'data.frame': 2 obs. of 4 variables:
#> $ Name: chr "A" "B"
#> $ gg_1: Factor w/ 2 levels "’No’","’Yes’": 2 1
#> $ gg_2: Factor w/ 2 levels "’No’","’Yes’": 1 2
#> $ gg_3: Factor w/ 2 levels "’No’","’Yes’": 2 2