The labelled
package provides this functionality to modify value labels for multiple variables in one go:
df <- data.frame(v1 = 1:3, v2 = c(2, 3, 1), v3 = 3:1)
val_labels(df[, c("v1", "v3")]) <- c(YES = 1, MAYBE = 2, NO = 3)
val_labels(df)
But I'm wondering if there's a way to do this in tidyverse syntax:
Something like this:
library(tidyverse)
df%>%
mutate(across(V1:V2), ~val_labels(.x)<-c(YES = 1, MAYBE = 2, NO = 3)
CodePudding user response:
We need to assign and then return the column (.x
). In addition, when there are more than one expression, wrap it inside the {}
library(dplyr)
library(labelled)
df <- df %>%
mutate(across(v1:v2, ~
{
val_labels(.x) <- c(YES = 1, MAYBE = 2, NO = 3)
.x
}))
-output
> val_labels(df)
$v1
YES MAYBE NO
1 2 3
$v2
YES MAYBE NO
1 2 3
$v3
NULL
CodePudding user response:
I would suggest using haven
's labelled class directly, alternatively check out the labelled
package's functions made for the dplyr
syntax, e.g. add_value_labels
.
df <-
df |>
mutate(across(v1:v2,
~ haven::labelled(.,
labels = c(YES = 1,
MAYBE = 2,
NO = 3)
)
)
)
labelled::val_labels(df)
Output:
$v1
YES MAYBE NO
1 2 3
$v2
YES MAYBE NO
1 2 3
$v3
NULL
A side note: Unless you have a very specific reason for using the labelled
-package I'd suggest that you keep the usage to a minimum and coerce into factors
, especially in the case of value labels. I've learned the hard way that the labelled
-package (and sjlabelled
for that matter) will often let you do things that seems smart on the outset but isn't in the long run.
A labelled vector is a common data structure in other statistical environments, allowing you to assign text labels to specific values. (...) This class provides few methods, as I expect you'll coerce to a standard R class (e.g. a factor()) soon after importing.
https://haven.tidyverse.org/reference/labelled.html (My emphasis)