Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created June 23, 2017 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpghayes/00c6875d18e7ed6a7b86e329eba65e09 to your computer and use it in GitHub Desktop.
Save alexpghayes/00c6875d18e7ed6a7b86e329eba65e09 to your computer and use it in GitHub Desktop.
tidyr::spread -- use `sep` and `convert` args for a good time
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