Skip to content

Instantly share code, notes, and snippets.

@ndricca
Created August 1, 2018 10:22
Show Gist options
  • Save ndricca/21a0aa181ff364a6bbcaf3931d506866 to your computer and use it in GitHub Desktop.
Save ndricca/21a0aa181ff364a6bbcaf3931d506866 to your computer and use it in GitHub Desktop.
how to make this join for multiple columns?
library(tidyverse)
(a <- data_frame(ID = 1:20,
D1 = sample(0:1,size = 20,replace = T,prob = c(0.8,0.2)),
D2 = sample(0:1,size = 20,replace = T,prob = c(0.7,0.3)))
)
(b <- data_frame(ID = 1:20,
D1 = sample(0:1,size = 20,replace = T,prob = c(0.9,0.1)),
D2 = sample(0:1,size = 20,replace = T,prob = c(0.9,0.1)))
)
full_join(a,b,by = "ID",suffix=c("_a","_b")) %>%
mutate(D1 = ifelse(D1_a + D1_b==2,1,D1_a + D1_b),
D2 = ifelse(D2_a + D2_b==2,1,D2_a + D2_b)) %>%
select(ID,D1_a,D1_b,D1,D2_a,D2_b,D2) %>%
mutate_all(as.integer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment