从 long table 为每个变量创建一个向量
create a vector from a long table for each variable
我有一个关注者table
Name Subject
Alex Math
Alex Physics
Ann History
Ann Literature
Ann Social Sciences
我想将此 table 转换为具有两行的 table,因此它看起来像:
Name Subject
Alex c(Math, Physics)
Ann c(History, Literature, Social Sciences)
此转换的目的是生成向量中元素的所有组合:
Math Physics
History Literature
History Social Sciences
Literature Social Sciences
您可以使用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
我有一个关注者table
Name Subject
Alex Math
Alex Physics
Ann History
Ann Literature
Ann Social Sciences
我想将此 table 转换为具有两行的 table,因此它看起来像:
Name Subject
Alex c(Math, Physics)
Ann c(History, Literature, Social Sciences)
此转换的目的是生成向量中元素的所有组合:
Math Physics
History Literature
History Social Sciences
Literature Social Sciences
您可以使用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