簡體   English   中英

如何根據R中的條件替換列值

[英]How to replace column values based on a condition in R

我有一個不同訂單的數據集以及該客戶訂單的數量。 我想從所有行中刪除 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")

也許使用tidyverse你可以這樣處理它:

使用pivot_longer更容易以長格式操作此數據。 您可以過濾掉不需要的行(同時刪除OrderTypeQUANTITY )。 然后pivot_wider如果這是所需的格式,根據需要填寫NA )。 我希望這是有幫助的。

編輯:對於每個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM