0

I want to convert multiple columns of a data.frame from the character class to the factor class while specifying the factor labels.

First I will create a dummy data.frame on which the code should such that others can reproduce the issue as follows:

df <- data.frame(replicate(2,sample(0:1,20,rep=TRUE)),
                 replicate(2,sample(0:2,20,rep=TRUE)))
names(df) <- c("xxx_var1", "xxx_var2", "yyy_var1", "yyy_var2")

I have managed to convert the desired columns as follows:

df <- df %>%
   mutate_at(vars(starts_with("xxx")), factor)

Now I want to specify arguments in the factor() function but I don't know how. If I try the following:

df <- df %>%
   mutate_at(vars(starts_with("xxx")), factor(labels = c("no", "yes"))

it returns the following error:

Error in factor(labels = c("no", "yes")) : invalid 'labels'; length 2 should be 1 or 0

Is it possible to specify the factor labels for all these columns at once using mutate_at?

5
  • Are you familiar with map functions from purrr package? Also, can you provide a sample of the data you are using to make the problem reproducible in our machines? You can do that using dput(head(df)) and copying the result from the console. Commented Dec 1, 2021 at 22:57
  • 2
    It would help to see a representative sample of the data in df as plain text e.g. the output from dput(df). Commented Dec 1, 2021 at 23:01
  • 2
    df %>% mutate_at(vars(starts_with("xxx")), factor, labels = c("no", "yes"))? Commented Dec 1, 2021 at 23:01
  • 1
    library(dplyr); df %>% mutate(across(starts_with("xxx"), ~factor(., levels = c("no", "yes")))) Commented Dec 1, 2021 at 23:26
  • @jdobres you solution worked, thanks a lot! Very clean and simple. If you could post your idea as an answer I can accept it as the proper solution. Commented Dec 2, 2021 at 10:04

1 Answer 1

3

Provide factor's "labels" argument as a named argument to mutate_at:

df %>% 
  mutate_at(vars(starts_with("xxx")), factor, labels = c("no", "yes"))

However, the mutate_xxx functions have been superseded by the use of mutate and the new across function. As Jon Spring commented, the following is equivalent:

df %>% 
  mutate(across(starts_with("xxx"), ~factor(., levels = c("no", "yes"))))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.