Last active
November 1, 2016 16:31
-
-
Save ivyleavedtoadflax/f685f2d96ee8b0aa35ea9d51543d1666 to your computer and use it in GitHub Desktop.
Example of using plyr::mapvalues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(plyr) | |
library(dplyr) | |
# Create a vector of numbers | |
num_vec <- round(runif(n = 100, min = 1, max = 26)) | |
# Convert these to letters using the inbuilt letters vector | |
letter_vec <- letters[num_vec] | |
# Print to check | |
num_vec | |
letter_vec | |
# Create a data.frame | |
df <- data_frame( | |
letter_column = letter_vec | |
) | |
df | |
# Now map the values back to numbers using the inbuilt letters vector | |
df %>% | |
mutate( | |
numbers = mapvalues( | |
letter_column, | |
from = letters, | |
to = 1:26 | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment