业务理解:使用
过滤数据Dataset_z$DayCount <= Dataset_z$t & Dataset_z$DayCount >= Dataset_z$Total.LT
然后按照以下条件在Dataset_z
中创建新列
group_by(Dataset_z$Source,Dataset_z$Plant, Dataset_z$Material) %>%
mutate(Dataset_z$A = min(NET.INV))
请帮助我
答案 0 :(得分:0)
我通过执行以下操作创建了一些示例数据:
library(dplyr)
Dataset_z <- data.frame(Total.LT = sample(1:6,10,replace=T),
DayCount = sample(1:10,10,replace=T),
t = sample(1:6,10,replace=T),
NET.INV = sample(1:20, 10, replace = T),
Source = sample(c("a", "b", "c", "d", "e"),10,replace=T),
Plant = sample(c("a", "b", "c", "d", "e"),10,replace=T),
Material = sample(c("a", "b", "c", "d", "e"),10,replace=T), stringsAsFactors = F)
DataSet_z看起来像:
Total.LT DayCount t NET.INV Source Plant Material
1 5 6 6 20 a c b
2 3 2 6 9 b c b
3 4 4 2 5 e e d
4 3 3 2 12 a c a
5 1 8 2 9 c d a
6 2 2 2 16 c b d
7 4 8 1 15 c b a
8 3 5 6 6 d a e
9 5 3 6 11 c b a
10 4 5 5 1 c e c
进行过滤和分组不需要使用$
运算符,因为dplyr使用%>%
运算符附加列。
Dataset_z %>% filter(DayCount >= Total.LT & DayCount <= t) %>% group_by(Source, Plant, Material) %>% mutate(A = min(NET.INV))
结果是:
Total.LT DayCount t NET.INV Source Plant Material A
<int> <int> <int> <int> <chr> <chr> <chr> <dbl>
1 5 6 6 20 a c b 20
2 2 2 2 16 c b d 16
3 3 5 6 6 d a e 6
4 4 5 5 1 c e c 1