R:如何扩展整齐 table 的排名词

R: How to expand a tidy table of ranked words

我有一个整洁的数据框

  > data.frame("topic" = c(1,1,2,2,3,3), 
               "term" = c("will", "eat", "go", "fun", "good", "bad"), 
               "score" = c(0.3, 0.2, 0.5, 0.4, 0.1, 0.05))

      topic term score
    1     1 will  0.30
    2     1  eat  0.20
    3     2   go  0.50
    4     2  fun  0.40
    5     3 good  0.10
    6     3  bad  0.05

因此 table 的目的是存储每个主题的前 n 个(在本例中为 2)得分项。这个 table 很容易使用,但我希望能够像这样查看数据:

      topic1  topic2  topic3
   1    will      go    good
   2     eat     fun     bad

在这个新的 table 中,我不关心得分,我只想查看每个主题的前 n 个得分项。我觉得这应该可以使用 dplyr 或其他东西来实现,但我对 R 不太满意。

library(reshape2)
dcast(df, ave(df$topic, df$topic, FUN = seq_along)~topic, value.var = "term")[,-1]
#     1   2    3
#1 will  go good
#2  eat fun  bad

library(dplyr)
bind_cols(lapply(split(df, df$topic), function(a) a["term"]))
#  term term1 term2
#1 will    go  good
#2  eat   fun   bad