如何根据R中的条件替换列值

问题描述 投票:0回答:1

我有一个不同订单的数据集以及该订单的数量给客户。我想从所有行中删除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")

这是除去“上限”和相应的数量值后输出的外观。。>

#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")

我有一个不同订单的数据集以及该订单的数量给客户。我想从所有行中删除ordertype ==“ cap”并为该订单形成相应的数量...

r data-mining data-extraction
1个回答
2
投票

也许使用tidyverse,您可以通过这种方式进行处理:

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.