Need help with mutate across many columns

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:

df<- df%>%
mutate(across(starts_with("'gg_")), as.factor,
levels = c(0,1),
labels = c("’No’", "’Yes’"))

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

Created on 2022-08-28 by the reprex package (v2.0.1)

2 Likes

Thank you sooo much! Works perfectly! :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.