Reputation: 103
I am trying to recode a group of variables in a dataframe(wide format). Let's say in that dataframe, I have a variable - "Education status" which runs from month 1 to 150, labeled as EDU1, EDU2, EDU3 .... EDU150 across 150 columns. The variable has values = 0,1,2,3,4,5,NA. I am trying to recode the variables (0=91,1=92,2=93,3,4,5=94) while keeping NA as it is. Since it is a large number of columns, I am trying to do it at once. This is what I tried.
First selecting columns from EDU1 to EDU150 at once.
library(dplyr)
df %>%
mutate_at(c(EDU1:EDU150),
~recode(.,"0=91;1=92,2=93;3=94;4=94;5=94"))
Another way I tried is picking up a part of the column name i.e starts with 'EDU'and specifying where to run the recode function.
library(dplyr)
df%>%
mutate(across(starts_with('EDU'), ~ recode(.,"0=91;1=92,2=93;3=94;4=94;5=94")))
None of this work, and I get an error saying
Unreplaced values treated as NA as.x is not compatible. Please specify replacements exhaustively or supply .default
Can anyone help me with this? Is it because I have some NA values in the variables I am working at or something is wrong with the code. What would be the best way to run a function selecting a large number of columns by picking up the column name?
Upvotes: 1
Views: 455
Reputation: 196
library(dplyr)
df %>%
mutate(across(
EDU1:EDU150,
~ recode(
.x,
`0` = 91L,
`1` = 92L,
`2` = 93L,
`3` = 94L,
`4` = 94L,
`5` = 94L
)
))
Upvotes: 1