Skip to content

Instantly share code, notes, and snippets.

@benlistyg
Created December 12, 2019 18:04
Show Gist options
  • Save benlistyg/7af3cfe82adb9f28ac50e32280515ad7 to your computer and use it in GitHub Desktop.
Save benlistyg/7af3cfe82adb9f28ac50e32280515ad7 to your computer and use it in GitHub Desktop.
Programmatically recode variables (for @lpsatchell)
# Generate Fake Data
fake_data <- sample(x = c(1:5), size = 100, replace = T) %>%
matrix(ncol = 10)
# We want to programmatically recode these.
# 5 to 1 (and vice versa)
# 4 to 2 (and vice versa)
recoding_function <- function(x){
(max(x) - x) + 1
}
recoded_fake_data <- apply(fake_data, 2, recoding_function)
fake_data
recoded_fake_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment