【发布时间】:2018-06-09 15:19:47
【问题描述】:
说,我有数据集。
structure(list(SKU = c(13284L, 13284L, 13284L, 13284L, 13284L,
13284L, 13284L, 13284L, 13284L, 13284L, 13284L), stuff = c(4565,
0, 0, 0, 567.0065222, 0, -1, 73.82897425, -1, 567.0065222, 614.2570658
), action = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), acnumber = c(329L,
329L, 329L, 329L, 329L, 329L, 329L, 329L, 329L, 329L, 329L),
year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L)), .Names = c("SKU", "stuff",
"action", "acnumber", "year"), class = "data.frame", row.names = c(NA,
-11L))
action 列只有两个值 0 和 1。 正如我们所见,1 类的东西有 1 次观察,0 类的东西有 10 次观察。
1.我必须计算最后三个观察值的中位数,但不需要在 Stuff 列中小于或等于零。所以我必须与最后三个 obs 一起工作。按 0 操作类别列出的内容。
567,0065222
73,8289742
567,0065222
the median =567,0065
-
现在,我必须取 1 个动作类别的单个值并从中减去计算出的中位数
614,2570658-567,0065222=47,2505436
我这样做
AwesomeData %>% {.[.$stuff>0,]} %>% {.[.$action==0,]} %>% tail(3) %>% {median(.$stuff)} -> OURMEDIANA
AwesomeData %>% {.[.$action==1,]} %>% {.$stuff}-OURMEDIANA -> WHATWENEED
a=cbind(AwesomeData,WHATWENEED)
但是如果我有两组呢 有些是这样的
structure(list(SKU = c(13284L, 13284L, 13284L, 13284L, 13284L,
13284L, 13284L, 13284L, 13284L, 13284L, 13284L, 13285L, 13285L,
13285L, 13285L, 13285L, 13285L, 13285L, 13285L, 13285L, 13285L,
13285L), stuff = c(4565, 0, 0, 0, 567.00652, 0, -1, 73.82897,
-1, 567.00652, 614.25707, 4565, 0, 0, 0, 567.00652, 0, -1, 73.82897,
-1, 567.00652, 614.25707), action = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L
), acnumber = c(329L, 329L, 329L, 329L, 329L, 329L, 329L, 329L,
329L, 329L, 329L, 330L, 330L, 330L, 330L, 330L, 330L, 330L, 330L,
330L, 330L, 330L), year = c(2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L)), .Names = c("SKU",
"stuff", "action", "acnumber", "year"), class = "data.frame", row.names = c(NA,
-22L))
现在我们有了新组
SKU +acnumber+year
13285+330+2017
如何将这个字符串用于数据集中的所有组
我看到的输出
SKU stuff action acnumber year new
<int> <dbl> <int> <int> <int> <dbl>
1 13284 4565 0 329 2018 3998
2 13284 0 0 329 2018 - 567
3 13284 0 0 329 2018 - 567
4 13284 0 0 329 2018 - 567
5 13284 567 0 329 2018 0
6 13284 0 0 329 2018 - 567
7 13284 - 1.00 0 329 2018 - 568
8 13284 73.8 0 329 2018 - 493
9 13284 - 1.00 0 329 2018 - 568
10 13284 567 0 329 2018 0
但我必须看到
SKU acnumber year result
13284 329 2018 47,25055
13285 330 2017 47,25055 614,25707-median of three last obs. Of zero (567,00652)
【问题讨论】:
-
不,我必须减去零类别的最后三个值
-
@akrun 我编辑了帖子,你能检查一下吗
-
我们在 stuff 列中删除所有小于或等于零的零类动作,而不是用最后一个 obs 计算中值。零类别。我的字符串做这个任务,但我需要按组
-
@akrun 我需要最后三个观察值的中位数
-
@akrun 是的,我想汇总一行数据