我的矩阵包含2列。第一列名称是模糊化的,第二列是relasifuzzi。
fuzzified relasifuzzi
1 2
2 3
3 5
5 9
9 8
9 10
9 7
我想组建一个这样的小组:
fuzzified relasifuzzi
1 2
2 3
3 5
5 9
9 8, 10, 7
我怎样才能在R?
中得到这样的结果答案 0 :(得分:2)
library(data.table)
setDT(k1)[,.(relasifuzzi=paste(relasifuzzi,collapse=",")),by=fuzzified]
fuzzified relasifuzzi
1: 1 2
2: 2 3
3: 3 5
4: 5 9
5: 9 8,10,7
k1<-structure(list(fuzzified = c(1L, 2L, 3L, 5L, 9L, 9L, 9L), relasifuzzi = c(2L,
3L, 5L, 9L, 8L, 10L, 7L)), .Names = c("fuzzified", "relasifuzzi"
), class = "data.frame", row.names = c(NA, -7L))
答案 1 :(得分:1)
假设您的数据位于名为“df”的data.frame
中,请尝试:
library("dplyr")
df %>% group_by(fuzzified) %>%
summarize(relasifuzzi = paste(relasifuzzi, collapse = ", "))
或:
library("plyr")
ddply(df, .(fuzzified), summarize, relasifuzzi = paste(relasifuzzi, collapse = ", "))