答案 0 :(得分:1)
也许您可以使用aggregate
+ replace
x <- aggregate(roll.no ~ ., df, FUN = function(v) paste0(v,collapse = "|"))
dfout <- within(x[order(x$subject,x$center),],
subject <-replace(subject,duplicated(subject),""))
这样
> dfout
subject center roll.no
1 100 2500 700|701|702
4 2501 703
2 101 2500 701
5 2501 705
3 102 2500 707
6 2502 701|700
数据
df <- structure(list(subject = c(100, 100, 100, 100, 101, 101, 102,
102, 102), center = c(2500, 2500, 2500, 2501, 2500, 2501, 2502,
2500, 2502), roll.no = c(700, 701, 702, 703, 701, 705, 701, 707,
700)), class = "data.frame", row.names = c(NA, -9L))
答案 1 :(得分:1)
我们可以在dplyr
中进行此操作。在按“主题”,“中心”分组之后,我们通过paste
collapse
中的|
和summarise
{{1 }}“主题”为空(replace
)
duplicated
或在''
中与library(dplyr)
df1 %>%
group_by(subject, center) %>%
summarise(rollno = paste(rollno, collapse="|")) %>%
ungroup %>%
mutate(subject = replace(subject, duplicated(subject), ""))
# A tibble: 6 x 3
# subject center rollno
# <chr> <dbl> <chr>
#1 "100" 2500 700|701|702
#2 "" 2501 703
#3 "101" 2500 701
#4 "" 2501 705
#5 "102" 2500 707
#6 "" 2502 701|700
base R
aggregate/replace/transform