【问题标题】:Is there an R function to find the instance number an element has appeared in a list based on certain conditions?是否有一个 R 函数可以根据特定条件查找某个元素在列表中出现的实例编号?
【发布时间】:2020-04-14 00:39:23
【问题描述】:

我正在尝试获取产品更改的正确实例计数并且正在苦苦挣扎。我觉得这应该很容易,但是对于我的生活,我今天无法弄清楚,这让我发疯了。

所以下面我有一些针对虚拟数据的示例代码:

library(tidyverse)
library(openxlsx)
library(olapR)
library(janitor)

file_path <- "C:\\Users\\user_name\\Desktop\\R_Question.xlsx"

df_file <- read.xlsx(file_path)

df_file <- df_file %>%
  clean_names() %>%
  mutate(actual_result = if_else((lag(product_type) == product_type &
                                  lag(claim_type) == claim_type &
                                  lag(date) != date),
                                  item_count + 1,
                                  item_count)
  ) %>% 
  replace(is.na(.), 1) %>% 
  mutate(actual_result = str_c("A", actual_result))

df_file

产生:

date        order     product      claim_type  item_count desired_result  actual_result
2019-12-01  QN123456  Jacket       Alteration       1         A1              A1
2019-12-07  QN123456  Jacket       Alteration       1         A2              A2
2019-12-11  QN123456  Pants        Alteration       1         A1              A1
2019-12-13  QN123456  Pants        Alteration       1         A2              A2
2019-12-18  QN123456  Pants        Alteration       1         A3              A2
2019-12-19  QN123456  Pants        Alteration       1         A4              A2

除了最后一列之外的所有内容都是读入的文件的一部分。最后一列是使用 mutate 添加的。我试图从 mutate to = 期望的结果列中获取实际结果,但我一直在“实际结果”列结束。

我尝试过使用 purr::map() + 函数以及 for 循环,但最终得到的结果与“actual_result”列相同。

我也尝试使用 cumsum(item_count) 代替 item_count + 1 但这不是我想要的,它会产生:

date        order     product      claim_type  item_count desired_result  actual_result
2019-12-01  QN123456  Jacket       Alteration       1         A1              A1
2019-12-07  QN123456  Jacket       Alteration       1         A2              A2
2019-12-11  QN123456  Pants        Alteration       1         A1              A1
2019-12-13  QN123456  Pants        Alteration       1         A2              A4
2019-12-18  QN123456  Pants        Alteration       1         A3              A5
2019-12-19  QN123456  Pants        Alteration       1         A4              A6

... 这很接近,但不是我需要的

有什么想法吗?

谢谢!

【问题讨论】:

    标签: r purrr dplyr


    【解决方案1】:

    您可能需要为每个productclaim_type 分配一个唯一编号。

    library(dplyr)
    
    df %>% 
        group_by(product, claim_type) %>% 
        mutate(actual_result = paste0('A', row_number()))
    
    
    #  date       order    product claim_type item_count desired_result actual_result
    #  <fct>      <fct>    <fct>   <fct>           <int> <fct>          <chr>        
    #1 2019-12-01 QN123456 Jacket  Alteration          1 A1             A1           
    #2 2019-12-07 QN123456 Jacket  Alteration          1 A2             A2           
    #3 2019-12-11 QN123456 Pants   Alteration          1 A1             A1           
    #4 2019-12-13 QN123456 Pants   Alteration          1 A2             A2           
    #5 2019-12-18 QN123456 Pants   Alteration          1 A3             A3           
    #6 2019-12-19 QN123456 Pants   Alteration          1 A4             A4           
    

    数据

    df <- structure(list(date = structure(1:6, .Label = c("2019-12-01", 
    "2019-12-07", "2019-12-11", "2019-12-13", "2019-12-18", "2019-12-19"
    ), class = "factor"), order = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "QN123456", class = "factor"), product = structure(c(1L, 
    1L, 2L, 2L, 2L, 2L), .Label = c("Jacket", "Pants"), class = "factor"), 
    claim_type = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Alteration", 
    class = "factor"),item_count = c(1L, 1L, 1L, 1L, 1L, 1L), 
    desired_result = structure(c(1L,2L, 1L, 2L, 3L, 4L), 
    .Label = c("A1", "A2", "A3", "A4"),class = "factor")),
    row.names = c(NA, -6L), class = "data.frame")
    

    【讨论】:

    • 感谢 Ronak 成功了。不知道为什么我以前从未想过使用 group_by。 row_number 对我来说是一个新的!你能解释一下为什么我最初使用 mutate 的尝试没有奏效吗?
    • 我认为这是因为当您在ifelse 中执行item_count + 1 时,它不会更新item_count 中上一次迭代的值。 item_count 始终保持为 1。您可能需要在某处使用 cumsum
    【解决方案2】:

    如果有任何 NA 值,我们可以使用 str_c 也将返回 NA

    library(dplyr)
    library(stringr)
    df %>% 
       group_by(product, claim_type) %>% 
       mutate(actual_result = str_c('A', row_number()))
    

    或与rowid 来自data.table

    library(data.table)
    setDT(df)[, actual_result := paste0("A", rowid(product, claim_type))][]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2020-03-29
      相关资源
      最近更新 更多