簡體   English   中英

從融合數據到長格式的Dcast聚合

[英]Dcast aggregate from melted data to long format

我成功完成了數據框的一個子集的操作,但似乎無法使其與其他子集一起使用。 有大約4000個訂單的信息,范圍為0-8個月,情緒為0-5。

目標是融合ID為“ order”和“ month.of.service”的數據,並匯總該月的平均情緒。 數據框如下所示:

order | month | sentiment
123   |   0   |     3
123   |   0   |     4
123   |   1   |     3
124   |   0   |     2

我希望它看起來像這樣:

123   |   0   |    3.5
123   |   1   |    3
124   |   0   |    2

這是我使用的實際代碼:

sentiment.md <- melt(sentiment, id = c('Related.order', 'Lifespan'))
sentiment.dc <- dcast(sentiment.md, Related.order + Lifespan ~ value, sum)

> head(sentiment.md)
  Related.order Lifespan  variable value
1         12771        0 Sentiment     5
2         11188        1 Sentiment     3
3         12236        3 Sentiment     5
4         12925        0 Sentiment     5
5         12151        3 Sentiment     5
6         12338        0 Sentiment     5

> head(sentiment.dc)
  Related.order Lifespan   0   1   2   3   4   5
1          4976        0 NaN NaN NaN   3 NaN NaN
2          4976        1 NaN NaN NaN   3 NaN NaN
3          4976        2 NaN NaN NaN NaN   4 NaN
4          4976        3 NaN NaN NaN NaN   4 NaN
5          4976        4 NaN NaN NaN NaN   4 NaN
6          4976        5 NaN NaN NaN NaN   4 NaN

為了進一步說明我希望它看起來像什么,這是使用數據框中我希望的格式的唯一其他列進行交互的完全相同的事情:

interactions.md <- melt(interactions, id = c('Related.order', 'Lifespan'))
interactions.dc <- dcast(interactions.md, Related.order + Lifespan ~ value, sum)

> head(interactions.md)
  Related.order Lifespan variable value
1         12771        0    Event     1
2         11188        1    Event     1
3         12236        3    Event     1
4         12925        0    Event     1
5         12151        3    Event     1
6         12338        0    Event     1
> head(interactions.dc)
  Related.order Lifespan 1
1          4976        0 6
2          4976        1 3
3          4976        2 3
4          4976        3 1
5          4976        4 2
6          4976        5 2

我以為也許我使用了錯誤的結構或某些東西,但無法識別任何東西。 供參考,這是R-studio的屏幕截圖:

在此處輸入圖片說明 在此先感謝您的幫助。

也許您想要進行更多的聚合/折疊而不是進行dcast

library(data.table);
setDT(df)[, .(sentiment = mean(sentiment)), by = .(order, month)]
#   order month  V1
#1:   123     0 3.5
#2:   123     1 3.0
#3:   124     0 2.0

如果您確實想使用dcast進行操作,則可以嘗試:

dcast(df, order + month ~ ., mean, value.var = "sentiment")

或使用dplyr

df %>% group_by(order, month) %>% summarise(sentiment = mean(sentiment))

這些只是R中許多聚合示例中的一些。


數據:

df <- structure(list(order = c(123L, 123L, 123L, 124L), month = c(0L, 
0L, 1L, 0L), sentiment = c(3L, 4L, 3L, 2L)), .Names = c("order", 
"month", "sentiment"), row.names = c(NA, -4L), class = "data.frame")

對於基數R,請使用aggregate

aggregate(sentiment ~ month + order, sentiment, mean, na.rm = TRUE)[c(2, 1, 3)]
#  order month sentiment
#1   123     0       3.5
#2   123     1       3.0
#3   124     0       2.0

數據。

sentiment <- read.table(text = "
order | month | sentiment
123   |   0   |     3
123   |   0   |     4
123   |   1   |     3
124   |   0   |     2
", header = TRUE, sep = "|")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM