【问题标题】:How to replace column values based on a condition in R如何根据R中的条件替换列值
【发布时间】:2019-11-24 22:21:18
【问题描述】:

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

这就是去掉“cap”和相应的数量值后输出的样子..

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

【问题讨论】:

  • 抱歉,我正在通话中错过了您的评论
  • @akrun 我只是指我之前发布的问题。和那个很相似,我想你会有一些建议。

标签: r data-extraction


【解决方案1】:

也许使用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

【讨论】:

  • 如果与任何其他数据集一起使用,是否应该更改“names_to”和“names_from”中的值?
  • names_to 创建一个临时的order 列,其中列中的值派生自列名,使用下划线_ 作为分隔符。如果另一个数据集使用具有下划线和序号的相同模式,它应该可以工作。 pivot_wider 中的names_from 使用了临时变量neworder,它只是表示过滤数据后的订单号,不需要更改。
猜你喜欢
  • 2020-04-24
  • 2021-08-16
  • 1970-01-01
  • 2017-09-17
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多