2

I'm trying to add the same value labels to multiple columns in a dataframe using the expss::val_lab function, but any labelling function in expss well work for me.

#Example data 
ex1 <- c(1,1,2,2,3,3,4,4,5,5) 
ex2 <- c(5,4,3,2,1,1,2,3,4,5) 
ex6 <- c(1,2,3,4,5,1,2,3,4,5) 
df <- data.frame(cbind(ex1, ex2, ex6)) 

#Some of my efforts that don't work 
val_lab(list (df$ex1, df$ex2, df$ex6)) <- c ("Awful"=1, "Bad"=2, "Ok"=3, "Good"=4, "Great"=5)  
val_lab(c("ex1","ex2","ex6")) <- c("Awful"=1, "Bad"=2, "Ok"=3, "Good"=4, "Great"=5)
let(df,val_lab(c("ex1", "ex2", "ex6")) <- c("Awful"=1, "Bad"=2, "Ok"=3, "Good"=4, "Great"=5))
df=df %>% let(val_lab(c("ex1", "ex2", "ex6")) <- c("Awful"=1, "Bad"=2, "Ok"=3, "Good"=4, "Great"=5))
df=df %>% let(val_lab(c("ex1", "ex2", "ex6")) := c("Awful"=1, "Bad"=2, "Ok"=3, "Good"=4, "Great"=5))
Mike
  • 23
  • 4

1 Answers1

1

You can apply the labels to the entire data frame like so:

val_lab(df) <- c("Awful" = 1, "Bad" = 2, "Ok" = 3, "Good" = 4, "Great" = 5) 

Or, if you want to name the columns:

val_lab(df[c("ex1", "ex2", "ex6")]) <- c("Awful"=1, "Bad"=2, "Ok"=3, "Good"=4, "Great"=5) 
Maël
  • 45,206
  • 3
  • 29
  • 67