I'm inexperienced with for loops and try to write for loop in R for several columns from a df dataset. Each column I'm interested in has values from 1 to 15 or NA. "names" is a list of names of those columns:
>names
[1] "score" "rate" "asset" "capital" "earning" "ast" "liquid" "profit" "assesment" "factor"
I want to replace their values with groups such as "1-5", "6-10", "11-15". I tried following code:
for (i in names){
df <- mutate(df, i =
ifelse(df$i >= 1 & df$i <= 5 , "1-5",
ifelse(df$i >= 6 & df$i <= 10, "6-10",
ifelse(df$i >= 11 & df$i <= 15, "11-15",NA)))))
}
But got an error:
Error: Column i must be length 2511 (the number of rows) or one, not 0
Could you please help/advice how to write such for loop and to solve this task.