[英]How to replace column values based on a condition in R
I have a data set of different orders and the quantity of that order for customers.我有一个不同订单的数据集以及该客户订单的数量。 I want to remove ordertype == "cap" from all the rows and the corresponding quantity for that order form the respective quantity column and replace it with the next values that does not correspond to "cap"
我想从所有行中删除 ordertype ==“cap”,并从相应的数量列中删除该订单的相应数量,并将其替换为与“cap”不对应的下一个值
#INPUT DATA
custID <- data.frame(c(1,2,3,4,5))
OrderType_1 <- data.frame(c("ball", "pen", "ball", "shuttle", "pen"))
OrderType_2 <- data.frame(c("pen", NA, "cap", "cap", "pen"))
OrderType_3 <- data.frame(c("cap", NA, "cap", "cap", NA))
OrderType_4 <- data.frame(c("shuttle", NA, "ball", "cap", NA))
OrderType_5 <- data.frame(c("pen", NA, "cap", "ball", NA))
QUANTITY_1 <- data.frame(c(2,3,4,5,6))
QUANTITY_2 <- data.frame(c(2, NA, 1, 3, 3))
QUANTITY_3 <- data.frame(c(3,NA,5,6,NA))
QUANTITY_4 <- data.frame(c(2,NA,3,5,NA))
QUANTITY_5 <- data.frame(c(2,NA,2,3, NA))
report <- cbind(custID, OrderType_1, OrderType_2, OrderType_3, OrderType_4,
OrderType_5, QUANTITY_1, QUANTITY_2, QUANTITY_3, QUANTITY_4, QUANTITY_5 )
report <- as.data.frame(report)
colnames(report) <- c("CustID", "OrderType_1", "OrderType_2", "OrderType_3",
"OrderType_4", "OrderType_5", "QUANTITY_1", "QUANTITY_2", "QUANTITY_3",
"QUANTITY_4", "QUNATITY_5")
This is how the output should look after removing "cap" and the corresponding quantity value..这是删除“上限”和相应数量值后输出的外观。
#OUTPUT DATA TYPE
custID <- data.frame(c(1,2,3,4,5))
OrderType_1 <- data.frame(c("ball", "pen", "ball", "shuttle", "pen"))
OrderType_2 <- data.frame(c("pen", NA, "ball", "ball", "pen"))
OrderType_3 <- data.frame(c("shuttle", NA, NA, NA, NA))
OrderType_4 <- data.frame(c("pen", NA, NA, NA, NA))
OrderType_5 <- data.frame(c(NA, NA, NA, NA, NA))
QUANTITY_1 <- data.frame(c(2,3,4,5,6))
QUANTITY_2 <- data.frame(c(2, NA, 3, 3, 3))
QUANTITY_3 <- data.frame(c(2,NA,NA,NA,NA))
QUANTITY_4 <- data.frame(c(2, NA,NA,5,NA))
QUANTITY_5 <- data.frame(c(NA,NA,NA,NA,NA))
report_1 <- cbind(custID, OrderType_1, OrderType_2, OrderType_3,
OrderType_4, OrderType_5, QUANTITY_1, QUANTITY_2, QUANTITY_3, QUANTITY_4,
QUANTITY_5 )
report_1 <- as.data.frame(report_1)
colnames(report_1) <- c("CustID", "OrderType_1", "OrderType_2",
"OrderType_3",
"OrderType_4", "OrderType_5", "QUANTITY_1", "QUANTITY_2", "QUANTITY_3",
"QUANTITY_4", "QUNATITY_5")
Maybe using tidyverse
you could approach it this way:也许使用
tidyverse
你可以这样处理它:
This data is easier to manipulate in long form using pivot_longer
.使用
pivot_longer
更容易以长格式操作此数据。 You can filter out the rows you don't want (removing both the OrderType
as well as the QUANTITY
).您可以过滤掉不需要的行(同时删除
OrderType
和QUANTITY
)。 Then pivot_wider
if that is the desired format, filling in NA
as needed).然后
pivot_wider
如果这是所需的格式,根据需要填写NA
)。 I hope this is helpful.我希望这是有帮助的。
Edit : For each CustID
I needed to reorder after filtering out unwanted orders.编辑:对于每个
CustID
我需要在过滤掉不需要的订单后重新排序。
library(tidyverse)
report %>%
pivot_longer(cols = -CustID,
names_to = c(".value", "order"),
names_sep = "_") %>%
filter(OrderType != "cap") %>%
group_by(CustID) %>%
mutate(neworder = row_number()) %>%
pivot_wider(id_cols = CustID,
names_from = c(neworder, neworder),
names_sep = "_",
values_from = c(OrderType, QUANTITY))
# A tibble: 5 x 9
# Groups: CustID [5]
CustID OrderType_1 OrderType_2 OrderType_3 OrderType_4 QUANTITY_1 QUANTITY_2 QUANTITY_3 QUANTITY_4
<dbl> <fct> <fct> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
1 1 ball pen shuttle pen 2 2 2 2
2 2 pen NA NA NA 3 NA NA NA
3 3 ball ball NA NA 4 3 NA NA
4 4 shuttle ball NA NA 5 3 NA NA
5 5 pen pen NA NA 6 3 NA NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.