我有一张下表
Name Subject
Alex Math
Alex Physics
Ann History
Ann Literature
Ann Social Sciences
我想将此表转换为具有两行的表,因此它看起来像:
Name Subject
Alex c(Math, Physics)
Ann c(History, Literature, Social Sciences)
此转换的目的是创建生成向量中元素的所有组合:
Math Physics
History Literature
History Social Sciences
Literature Social Sciences
答案 0 :(得分:0)
您可以使用aggregate()
功能
dat=read.table(text="Name Subject
Alex Math
Alex Physics
Ann History
Ann Literature
Ann 'Social Sciences'",h=T,stringsAsFactors=F)
aggregate(Subject~Name,dat,I)
Name Subject
1 Alex Math, Physics
2 Ann History, Literature, Social Sciences