如何在列中合并具有相同字符的对象?

时间:2019-03-09 00:57:28

标签: r

比方说,我的DF只有1列“类型” 在该类型中,有“恐怖”,“浪漫”,“小说”等。

类型

Horror Horror Romance Fiction Romance

如何编码,以便可以将“恐怖”对象组合为一个? 所以基本上,我希望表格显示

类型

Horror Romance Fiction

预先感谢

2 个答案:

答案 0 :(得分:0)

unique(DF$Genre)

DF %>%
distinct(Genre)

答案 1 :(得分:0)

您可以使用aggregate函数获取要查找的表。

## Your sample data
DF = read.table(text="Genre Score 
Horror 4 
Horror 6
Romance 7 
Fiction 3 
Romance 10", 
header = TRUE)

整理桌子

TAB = aggregate(DF$Score, list(DF$Genre), mean)
names(TAB) = c("Genre", "MeanScore")
TAB
    Genre MeanScore
1 Fiction       3.0
2  Horror       5.0
3 Romance       8.5