id:客户
name:客户联系人姓名
phone:客户联系人手机号

library(plyr)
library(tidyverse)

1 创建数据


df <- data.frame(id = sort(c(rep(letters[1:3], each = 2), 'a', 'a', 'b', 'd')),
                 name = LETTERS[1:10],
                 phone = 0:9); df

2 方法一


df <- df %>% group_by(id) %>% mutate(number = 1:length(id)); df
df_1 <- df %>% filter(number == 1) %>% select(-number)
df_2 <- df %>% filter(number == 2) %>% select(-number)
df_3 <- df %>% filter(number == 3) %>% select(-number)
df_4 <- df %>% filter(number == 4) %>% select(-number)

df_1 %>%
  left_join(df_2, by = 'id') %>%
  left_join(df_3, by = 'id') %>%
  left_join(df_4, by = 'id')

3 方法二


【别运行】我记得你当时用过这个批量join,我这样写就崩了!!!

# dfs <- list(
#   df_1 <- df %>% filter(number == 1) %>% select(-number),
#   df_2 <- df %>% filter(number == 2) %>% select(-number),
#   df_3 <- df %>% filter(number == 3) %>% select(-number),
#   df_4 <- df %>% filter(number == 4) %>% select(-number)
# )
# 
# join_all(dfs, 'id')