Created
June 23, 2017 01:04
-
-
Save alexpghayes/00c6875d18e7ed6a7b86e329eba65e09 to your computer and use it in GitHub Desktop.
tidyr::spread -- use `sep` and `convert` args for a good time
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(tidyverse) | |
data_frame(id = 1:50, | |
rel1 = sample(LETTERS[1:4], 50, replace = TRUE), | |
gender1 = sample(c("M", "F", "O"), 50, replace = TRUE), | |
score1 = rnorm(50), | |
rel2 = sample(LETTERS[1:4], 50, replace = TRUE), | |
gender2 = sample(c("M", "F", "O"), 50, replace = TRUE), | |
score2 = rnorm(50)) %>% | |
gather(field, value, -id) %>% | |
spread(field, value, sep = "", convert = TRUE) %>% | |
mutate(avg_score = map2_dbl(fieldscore1, fieldscore2, ~mean(c(.x, .y)))) %>% | |
View() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment