【发布时间】:2020-01-08 12:45:44
【问题描述】:
我有原始数据集。下面的原始数据集是样本数据,有时间和情绪(正面、中性、负面)。
这个原始数据集是:
created_time neg_sentiment neu_sentiment pos_sentiment
2015-01-12T23:27:53+0000 0 0 1
2015-01-13T00:36:15+0000 0 0 1
2015-01-13T00:39:37+0000 0.02 0 0.98
2015-01-13T01:26:05+0000 0.41 0.59 0
2015-01-15T16:10:46+0000 0.14 0.02 0.84
2015-02-13T02:38:59+0000 0.86 0.1 0
2015-01-13T21:00:15+0000 1 0 0
2015-01-14T04:47:47+0000 0.96 0.04 0
2015-02-14T06:09:17+0000 1 0 0
2015-02-14T06:10:05+0000 1 0 0
2015-01-14T06:44:47+0000 0.65 0.3 0
2015-03-14T06:47:13+0000 0.07 0.93 0
2015-01-14T10:16:09+0000 0 0 1
2015-01-14T10:17:38+0000 0.08 0.85 0.07
2015-01-14T17:30:03+0000 1 0 0
2015-01-14T20:17:43+0000 0.11 0 0.89
2015-01-16T02:49:13+0000 0.5 0.5 0
2015-03-26T13:20:06+0000 1 0 0
2015-01-21T04:26:45+0000 0.39 0.01 0.6
2015-03-21T04:38:49+0000 0.01 0 0.99
使用这个数据集,我想做两个期望的输出:
negative_proportion 由 neg_sentiment/(neg_sentiment + neu_sentiment + pos_sentiment) 计算 第一个输出是按月份:
created_time negative_proportion
2015-01 10
2015-02 20
2015-03 5
第二个输出是按天:
created_time negative_proportion
2015-01-12 10
2015-01-13 20
2015-01-14 3
2015-01-15 3
2015-01-16 3
2015-02-13 3
2015-02-14 3
2015-03-14 3
2015-03-21 3
2015-03-26 5
我怎样才能做出想要的输出? 你能帮我或建议代码吗?
基于原始数据集生成的“dput”数据如下
structure(list(created_time = structure(c(1L, 2L, 3L, 4L, 12L,
15L, 5L, 6L, 16L, 17L, 7L, 18L, 8L, 9L, 10L, 11L, 13L, 20L, 14L,
19L), .Label = c("2015-01-12T23:27:53+0000", "2015-01-13T00:36:15+0000",
"2015-01-13T00:39:37+0000", "2015-01-13T01:26:05+0000", "2015-01-13T21:00:15+0000",
"2015-01-14T04:47:47+0000", "2015-01-14T06:44:47+0000", "2015-01-14T10:16:09+0000",
"2015-01-14T10:17:38+0000", "2015-01-14T17:30:03+0000", "2015-01-14T20:17:43+0000",
"2015-01-15T16:10:46+0000", "2015-01-16T02:49:13+0000", "2015-01-21T04:26:45+0000",
"2015-02-13T02:38:59+0000", "2015-02-14T06:09:17+0000", "2015-02-14T06:10:05+0000",
"2015-03-14T06:47:13+0000", "2015-03-21T04:38:49+0000", "2015-03-26T13:20:06+0000"
), class = "factor"), neg_sentiment = c(0, 0, 0.02, 0.41, 0.14,
0.86, 1, 0.96, 1, 1, 0.65, 0.07, 0, 0.08, 1, 0.11, 0.5, 1, 0.39,
0.01), neu_sentiment = c(0, 0, 0, 0.59, 0.02, 0.14, 0, 0.04,
0, 0, 0.35, 0.93, 0, 0.85, 0, 0, 0.5, 0, 0.01, 0), pos_sentiment = c(1,
1, 0.98, 0, 0.84, 0, 0, 0, 0, 0, 0, 0, 1, 0.07, 0, 0.89, 0, 0,
0.6, 0.99)), class = "data.frame", row.names = c(NA, -20L))
【问题讨论】:
-
使用
format创建2个新列;一个是format(your_date_var, '%Y-%m'),一个是format(your_date_var, '%Y-%m-%d')。然后使用这两个变量进行分组