I have a data set that has over 260 columns that have character values that need to be recoded as numerical factors. For example, N=0
, Y=1
, S=1
, O=2
, F=99
. However, apply this to only some columns, not all, so I need to do this by column name. Here is what I have so far:
df <- data.frame(col1 = c("N", "N", "O", "N", "N", "N", "N", "N", "S", "O"),
col2 = c("F", "N", "S", "O", "Y", "Y", "S", "F", "S", "O"),
col3 = c("Y", "O", "Y", "N", "Y", "F", "N", "O", "S", "N"),
col4 = c("S", "S", "O", "O", "Y", "S", "N", "O", "S", "N"),
col5 = c("N", "S", "F", "O", "N", "F", "N", "O", "N", "N"),
col6 = c("O", "N", "S", "N", "N", "N", "N", "F", "N", "O"),
col7 = c("F", "F", "O", "O", "O", "N", "N", "O", "Y", "Y"),
col8 = c("O", "N", "O", "S", "F", "N", "N", "Y", "S", "N"),
col9 = c("N", "O", "O", "Y", "N", "S", "N", "O", "N", "Y"),
col10 = c("N", "N", "S", "F", "N", "N", "F", "Y", "N", "O"))
df %>% mutate_at(.vars = c("col1", "col4", "col9"), funs(ifelse(*** == "N", 0, .)))
The 3 astericks indicate the problem. I have to specify the column name to make the recode. I run into the same problem in base R. Is there a way to do it to tell R that "these columns by name when equal to 'N' make 0".
I ended up creating a function with a for-loop, looping through the names of the columns.