Programmatic text substitution across rows and columns in R
You could first create a mapping using a named vector:

map &lt;- c("apple" = 1, "banana" = 1, "gum" = 2, "steak" = 2)


Then create a vector of all the variable names that need to be modified:

vars &lt;- c(paste0("food_day_", 1:7),
          paste0("otherfood_day_", 1:10))


Then modify the data as follows:

example[vars] &lt;- map[unlist(example[vars])]


(where unlist transforms the data.frame example[vars] into a single long vector.)

An interesting aspect of that approach, apart from its conciseness, is that it guarantees than the output columns become numeric. Whereas your approach, by making incremental changes to the columns, would preserve the original class of your columns, i.e. "character". Also note that foods that are not included in the map vector will be turned to NA since for example map["pineapple"] is NA.