I would like to find an efficient way to split and merge back my data.
I want to split my data by sex
and merged it back by household id (idhouse
). I am doing this to get a paired
database (so, I want to remove all the "singles").
What I have is this
idhouse idperso sex score
1 1 1 man 18
2 1 2 woman 22
3 2 1 man 19
4 2 2 woman 24
5 3 1 woman 30
want I want is this :
idhouse idperso_man score_man idperso_woman score_woman
1 1 18 2 22
2 1 19 2 24
I am having a mistake using map
and I can't figure what it is
library(dplyr)
library(purrr)
dt %>% split(.$sex) %>%
map_df(~merge(., .$man, .$woman, by = 'idhouse'))
data
dt = structure(list(idhouse = structure(c(1L, 1L, 2L, 2L, 3L), .Label = c("1",
"2", "3"), class = "factor"), idperso = structure(c(1L, 2L, 1L,
2L, 1L), .Label = c("1", "2"), class = "factor"), sex = structure(c(1L,
2L, 1L, 2L, 2L), .Label = c("man", "woman"), class = "factor"),
score = structure(c(1L, 3L, 2L, 4L, 5L), .Label = c("18",
"19", "22", "24", "30"), class = "factor")), .Names = c("idhouse",
"idperso", "sex", "score"), row.names = c(NA, -5L), class = "data.frame")