我需要折叠B中的值:
A B
1 xyz
2 opi
1 abc
我需要以下输出:
A B
1 xyz / abc
2 opi
答案 0 :(得分:2)
aggregate(your_data_frame[2], your_data_frame[1], paste, collapse = " / ")
答案 1 :(得分:0)
tidyverse
使用dplyr的方法:
library(dplyr)
x <- read.table(text = "A B
1 xyz
2 opi
1 abc",header =TRUE)
x %>% group_by(A) %>% summarise(B = paste(B, collapse = " / "))
## A tibble: 2 x 2
# A B
# <int> <chr>
#1 1 xyz / abc
#2 2 opi