使用两列对数据进行分组,然后将第三列合并为一行

时间:2020-02-15 17:59:20

标签: r

我有这种形式的数据

enter image description here

我想用R作为Ouput

enter image description here

2 个答案:

答案 0 :(得分:1)

也许您可以使用aggregate + replace

尝试以下基本R代码
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
相关问题