R行到列

时间:2016-12-16 10:12:01

标签: r

使用R和dplyr,我有一个汇总的数据框,每天和每位用户都有情绪分数:

date       | user | sentiment_score | anger | fear | trust
2016-12-15 | x    | -1              | 2     | 1    | 0
2016-12-16 | x    | -1              | 0     | 0    | 0
2016-12-15 | y    | 1               | 0     | 1    | 0
2016-12-16 | y    | -1              | 3     | 0    | 0
2016-12-15 | z    | 0               | 0     | 0    | 0

Dput:

df <- structure(list(created_at = structure(c(17150, 17151, 17150, 
17151, 17150), class = "Date"), user = c("x", "x", "y", "y", 
"z"), sentiment_score = c(-1, -1, 1, -1, 0), anger = c(2, 0, 
0, 3, 0), fear = c(1, 0, 1, 0, 0), trust = c(0, 0, 0, 0, 0)), .Names = c("created_at", 
"user", "sentiment_score", "anger", "fear", "trust"), class = "data.frame", row.names = c("1900961", 
"1900971", "1900981", "1900991", "1901001"))

出于预测目的,我想将此数据帧转换为每天的数据帧,并为每个用户提供单独的情绪分数列(最后列有很多列):

date       | x_sentiment_score | x_anger | x_fear | x_trust| y_sentiment_score | y_anger | y_fear | y_trust| z_sentiment_score | z_anger | z_fear | z_trust
2016-12-15 | -1                | 2       | 1      | 0      | 1                 | 0       | 1      | 0      | 0                 | 0       | 0      | 0
2016-12-16 | -1                | 0       | 0      | 0      | -1                | 3       | 0      | 0      | 0                 | 0       | 0      | 0

我很困惑如何实现这一目标。任何建议都将不胜感激。

0 个答案:

没有答案