'mutate columns with labels attributes in r

I have imported data from SPSS and I want to transform some variables to factors, without having to list them all. These variables can be distinguished from the others by having the attribute labels, so I want to use the condition length(get_labels(variable.name))>0 to mutate (or, if not possible, to select) them.

Using the following example (despite not being a tbl_df as would be if imported with haven::read_sav)

dt <- data.table(a = 1:4, 
                 b = factor(c(1,2,1,1), levels=1:2, labels=c("Yes","No")),
                 d = c("xxx", "yyy", "zzz", "kkk"),
                 e = factor(c(1,1,3,2), levels=1:4, labels=c("A","B","C","D")))

I have tried

  • dt %>% mutate_at(vars(length(get_labels(.))>0), haven::as_factor)
  • dt %>% mutate_if((length(get_labels(.))>0), haven::as_factor)

but that didn't work.

How can I achieve the desired result, that is, mutate/select the columns band e?

Thanks!



Solution 1:[1]

I recommend selection before mutating. Something like the following to get all the columns of interest:

all_cols = colnames(dt)
focus_cols = all_cols[{logic that returns T/F for each column you want}]

You can then iterate over all such columns as follows:

for(cc in focus_cols){
  dt = mutate(dt, !!sym(cc) := as.factor(!!sym(cc)))
}

Where !!sym(cc) transforms the string stored in cc into a variable, and := is equivalent to = but allows us to use !!sym(.) on the left side too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon.S.A.