Мы можем использовать tidyverse
. После группировки по 'project_id', paste
first
'member_id' с остальными в list
, unnest
и удаление столбца 'project_id' с select
library(dplyr)
library(tidyr)
df1 %>%
group_by(project_id) %>%
summarise(newcol = list(paste(first(participant_id),
participant_id[-1], sep="."))) %>%
unnest %>%
select(-project_id)
# A tibble: 6 x 1
# newcol
# <chr>
#1 s01.s02
#2 s01.s05
#3 s02.s03
#4 s02.s06
#5 s02.s09
#6 s10.s19
Данные
df1 <- structure(list(project_id = c(1, 1, 1, 2, 2, 2, 2, 3, 3),
participant_id = c("s01",
"s02", "s05", "s02", "s03", "s06", "s09", "s10", "s19")),
class = "data.frame", row.names = c(NA,
-9L))