[英]Substract values in a column based on the values of 2 other columns
I have the dataframe below我有下面的 dataframe
vol<-structure(list(activity = c("RAAMELK", "RAAMELK", "Separering",
"Separering", "Sweetmilk Pasteurizer 8331", "Sweetmilk Pasteurizer 8331",
"9004 - T42 kartong 70x70", "9004 - T42 kartong 70x70", "9006 - T61 BIB",
"9006 - T61 BIB", "9004 - T41 kartong 70x70", "9004 - T41 kartong 70x70"
), qty = c(0, 31, 31, 9, 31, 31, 6, 6, 3, 3, 28, 28), in_out = c("in",
"out", "in", "out", "in", "out", "in", "out", "in", "out", "in",
"out"), qty_scrap = c(0, 0, 0, 0, 0, -270.64, 0, 524, 0, 260,
0, 0)),
row.names = c(NA, -12L), case_id = "case_id", activity_id = "activity",
activity_instance_id = "action", lifecycle_id = "registration_type",
resource_id = "resource", timestamp = "timestamp",
class = c("eventlog", "log", "tbl_df", "tbl", "data.frame"))
And I want to subtract the values on column qty
based on if they have the same value in column activity
and with the logic in-out
from the in_out
column.我想根据列activity
中的值是否相同以及in_out
列in-out
的逻辑输入输出来减去列qty
上的值。 The result will be saved in the qty_scrap
column.结果将保存在qty_scrap
列中。
The result dataframe will then have 4 columns.结果 dataframe将有 4 列。 The activity
column with the unique name of the activity
, the in
column with the in
value of the qty
, the out
column with the out
value of the qty
and the qty_scrap
column with their difference.具有activity
activity
in
具有in
值的qty_scrap
列,具有qty
out
值的out
列以及具有它们差异的qty
列。
You can reshape your data to a wider format, and then mutate your scrape column by subtracting the in and out columns.您可以将数据重塑为更宽的格式,然后通过减去输入和输出列来改变您的抓取列。
library(dplyr)
library(tidyr)
vol %>%
pivot_wider(id_cols = "activity", names_from = "in_out", values_from = "qty") %>%
mutate(qty_scrap = `in` - out)
#> # A tibble: 6 x 4
#> activity `in` out qty_scrap
#> <chr> <dbl> <dbl> <dbl>
#> 1 RAAMELK 0 31 -31
#> 2 Separering 31 9 22
#> 3 Sweetmilk Pasteurizer 8331 31 31 0
#> 4 9004 - T42 kartong 70x70 6 6 0
#> 5 9006 - T61 BIB 3 3 0
#> 6 9004 - T41 kartong 70x70 28 28 0
Created on 2023-01-24 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2023-01-24
You could try this:你可以试试这个:
vol %>%
group_by(activity) %>%
mutate(qty_scrap = head(qty, 1) - tail(qty, 1)) %>%
tidyr::pivot_wider(names_from = in_out, values_from = qty) %>%
select(activity, `in`, out, qty_scrap)
Output Output
# activity `in` out qty_scrap
# <chr> <dbl> <dbl> <dbl>
# 1 RAAMELK 0 31 -31
# 2 Separering 31 9 22
# 3 Sweetmilk Pasteurizer 8331 31 31 0
# 4 9004 - T42 kartong 70x70 6 6 0
# 5 9006 - T61 BIB 3 3 0
# 6 9004 - T41 kartong 70x70 28 28 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.