我在亚马逊只有一列食物评级的R中有一个df。
head(food_ratings)
product.productId..B001E4KFG0
1 review/userId: A3SGXH7AUHU8GW
2 review/profileName: delmartian
3 review/helpfulness: 1/1
4 review/score: 5.0
5 review/time: 1303862400
6 review/summary: Good Quality Dog Food
行重复自己,因此第7行到第12行具有关于另一个用户的相同信息(第7行)。这种模式重复多次。
因此,我需要将每组6行分布在一行中,每行6列,以便稍后我可以根据他们的评论/分数对查看/摘要进行分组。
我正在使用RStudio 1.0.143
编辑:我被要求显示dput(head(food_ratings, 24))
的输出,但无论使用的数量是多少,它都太大了。
非常感谢
答案 0 :(得分:2)
我已经获取了您的数据,并为其添加了2个虚假用户。使用tidyr
和dplyr
,您可以创建新列并将数据折叠为精美的data.frame。如果您不需要,可以使用select
中的dplyr
删除ID列,或者重新排列列的顺序。
library(tidyr)
library(dplyr)
df %>%
separate(product.productId..B001E4KFG0, into = c("details", "data"), sep = ": ") %>%
mutate(details = sub("review/ ", "", details)) %>%
group_by(details) %>%
mutate(id = row_number()) %>%
spread(details, data)
# A tibble: 3 x 7
id helpfulness profileName score summary time userId
<int> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 1/1 delmartian 5.0 Good Quality Dog Food 1303862400 A3SGXH7AUHU8GW
2 2 1/1 martian2 1.0 Good Quality Snake Food 1303862400 123456
3 3 2/5 martian3 5.0 Good Quality Cat Food 1303862400 123654
数据:
df <- structure(list(product.productId..B001E4KFG0 = c("review/userId: A3SGXH7AUHU8GW",
"review/profileName: delmartian", "review/helpfulness: 1/1",
"review/score: 5.0", "review/time: 1303862400", "review/summary: Good Quality Dog Food",
"review/userId: 123456", "review/profileName: martian2", "review/helpfulness: 1/1",
"review/score: 1.0", "review/time: 1303862400", "review/summary: Good Quality Snake Food",
"review/userId: 123654", "review/profileName: martian3", "review/helpfulness: 2/5",
"review/score: 5.0", "review/time: 1303862400", "review/summary: Good Quality Cat Food"
)), class = "data.frame", row.names = c(NA, -18L))