Automation of labelling of a set of variables with the same levels and labels

Hi everybody,

I'm a new R learner and also new on this community.

I'm trying to label some ordinal variables in my data frame. Several variables have the same levels and labels.

One example is bellow:
(a3$w1 <- ordered(a3$w1,
levels = c(1,2,3,4,5,6),
labels = c("Strongly disagree","Moderately Disagree", "Slightly disagree", "Slightly agree", "Moderately Agree","Strongly agree")))

I have in the data frame w1-w4 and also another vector nep1-nep16 with the same level and label.
Is there any way to avoid repeating the same code so many times for each variable?

I would like a command than can allow to do it for all similar variables at the same time
Thank you in advance

You can do something like this

library(dplyr)

a3 %>% 
    mutate(across(starts_with("w"),
                  ~ ordered(.,
                            levels = c(1,2,3,4,5,6),
                            labels = c("Strongly disagree","Moderately Disagree",
                                       "Slightly disagree", "Slightly agree",
                                       "Moderately Agree","Strongly agree")
                            )
                  )
           )

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you Andresrcs,

But it doesn't work.

Can you be more specific?, "it doesn't work." doesn't give us much information to help you, you have to tell us, what have you tried and what error message you are getting. As I said before, please provide a proper reproducible example illustrating your issue.

Sure.
Below is a screenshot showing the command and what I got in RStudio.

At the right side, you have a sample of the data with variables w1-w4 for seven observations.

When I ran your code, I didn't get any error message, but the variables are still integer not factor.

I used the following codes to check the type of data after running the code.
typeof(a3$w1)
str(a3)

May be I did something wrong.

Please, let me know if you need any additional information.

Thank you

i think you omitted the <- symbol which is used to assign a result to a variable

maybe

a4 <- a3 %>% mutate( ****....)

etc.
then the result of the mutate action will be seen in a4

Most R functions do not perform "in place" modifications, you have to explicitly assign them to a variable if you want the changes to persist.

library(dplyr)

a3 <- a3 %>% 
   mutate(across(starts_with("w"),
                 ~ ordered(.,
                           levels = c(1,2,3,4,5,6),
                           labels = c("Strongly disagree","Moderately Disagree",
                                      "Slightly disagree", "Slightly agree",
                                      "Moderately Agree","Strongly agree")
                           )
                 )
          )

Thank you very much.

It works

Thank you a lot for the help.

It works

This topic was automatically closed 21 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.