I am having problems recoding multiple columns in R. This is the code I have so far:
library(car)
c1 <- sample(1:5, 100, TRUE)
c2 <- sample(1:5, 100, TRUE)
c3 <- sample(1:5, 100, TRUE)
data <- data.frame(c1,c2,c3)
col_names <- colnames(data[c(1,3)])
for (name in col_names) {
data$name <- recode(data$name, "1=5; 2=4; 3=3; 4=2;5=1")
}
Everything works until the for-loop. The error says
Error: Assigned data `recode(df_meme$name, "1=5; 2=4; 3=3; 4=2;5=1")` must be compatible with existing data.
x Existing data has 203 rows.
x Assigned data has 0 rows.
i Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
Zusätzlich: Warnmeldung:
Unknown or uninitialised column: `name`.
I would greatly appreciate an answer! Thank you.
data[[name]]instead ofdata$namein thefor-loop.