Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set.seed(1)
- df1 <- data.frame(id = sample(LETTERS,50,replace=T), val = rnorm(50), val1 = rnorm(50), stringsAsFactors = F)
- df2 <- data.frame(id = sample(LETTERS,30,replace=T), val = rnorm(30), val2 = rnorm(30), stringsAsFactors = F)
- df.list <- list(df1,df2)
- library(dplyr)
- df <- df.list %>% purrr::reduce(dplyr::inner_join,by="id")
- id val.x val1 val.y val2
- 1 G -0.05612874 0.2914462 2.087167 0.7876396
- 2 G -0.05612874 0.2914462 -0.255027 1.4411577
- 3 J -0.15579551 -0.4432919 -1.286301 1.0273924
- df.list.colnames <- unlist(lapply(df.list,function(l) colnames(l %>% dplyr::select(-id))))
- df.list.colnames <- table(df.list.colnames)
- repeating.colnames <- names(df.list.colnames)[which(df.list.colnames > 1)]
- repeating.colnames.df <- do.call(rbind,lapply(df.list,function(r) r %>% dplyr::select_(.dots = c("id",repeating.colnames)))) %>%
- unique()
- for(r in 1:length(df.list)) df.list[[r]] <- df.list[[r]] %>% dplyr::select_(.dots = paste0("-",repeating.colnames))
- df <- df.list %>% purrr::reduce(dplyr::inner_join,by="id")
Add Comment
Please, Sign In to add comment