Принимая столбец 'семестр' как list
, сгруппированный по 'семестр', unlist
'курс' и затем summarise
как list
library(tidyverse)
df %>%
group_by(semester) %>%
summarise(course = list(unique(unlist(course))))
Или используйте aggregate
из base R
aggregate(df['course'], df['semester'], FUN = function(x) list(unique(unlist(x))))
# semester course
#1 1 math00, Geo00
#2 2 phys1, math02
#3 3 NA
#4 4 eng00
Данные
df <- data.frame(course = I(list('math00', 'phys1', NA, 'eng00',
c('math00', 'Geo00'), 'math02',NA)), semester = c(1, 2, 3, 4, 1, 2, 3))