【问题标题】:filter dataframe value using multiple dataframes使用多个数据帧过滤数据帧值
【发布时间】:2019-07-24 19:39:58
【问题描述】:

在 R 中可以做到这一点吗?有人可以指导我如何做到这一点

对于每个 Item-LC 组合,我需要根据 x 月的值从 df2 中过滤相应的值。

df1

     Item    LC   Fiscal.Month  fcst
1   0S1576  MW92    2019-M06    22
2   0S1576  MW92    2019-M06        18
3   0S1576  RM11    2019-M06    12
4   0S1576  MW92    2019-M07    10
5   0S1576  RM11    2019-M07    10
6   0S1576  MW92    2019-M08    12
7   0S1576  MW92    2019-M09    10

df2

     Item    LC  xmonths
1   0S1576  MW92    3
2   0S1576  RM11    1

df3

Currentmonth
2019-M06

假设 0S1576 MW92 我有 xmonths=3 然后从 Curretmonth 2019-M06 它应该选择 3 行作为 df1 的输出,对于 RM11 我们需要 1 行:

输出:

     Item    LC   Fiscal.Month  fcst
1   0S1576  MW92    2019-M06    22
2   0S1576  MW92    2019-M06        18
3   0S1576  RM11    2019-M06    12
4   0S1576  MW92    2019-M07    10
5   0S1576  MW92    2019-M08    12

【问题讨论】:

  • 这种逻辑的问题是,如果 df1 包含的条目多于 xmonths 指定的列,那么您不知道要提取哪些条目,除非这些数字始终匹配,在这种情况下您不知道需要xmonths 并且内部连接就足够了
  • df1 将包含比 xmonths 列更多的条目,因为这是要求。要提取的条目是当前月份的下 3 个条目。我是否也可以定义一个新的数据框,我将在其中按顺序指定所有会计月份。那么我们可以从df1中选择xmonths吗??

标签: r


【解决方案1】:
require(dplyr)
require(lubridate)



df1 <- read.table(text = 
  ' Item    LC   Fiscal.Month  fcst
1   0S1576  MW92    2019-M06    22
2   0S1576  MW92    2019-M06        18
3   0S1576  RM11    2019-M06    12
4   0S1576  MW92    2019-M07    10
5   0S1576  RM11    2019-M07    10
6   0S1576  MW92    2019-M08    12
7   0S1576  MW92    2019-M09    10') 



df2 <- 
  read.table(text = 'Item    LC  xmonths
1   0S1576  MW92    3
2   0S1576  RM11    1')





df3 <- read.table(text = 
                    'Currentmonth
2019-M06', header  = TRUE)




  df1 %>%  
    mutate(Currentmonth = df3$Currentmonth) %>%   ## adding current month 
    left_join(df2)  %>%   
    mutate(Fiscal.Month2 = gsub('M','', Fiscal.Month), 
           Currentmonth2 = gsub('M','', Currentmonth)) %>%  
    mutate(Fiscal.Month2  = paste(Fiscal.Month2, '-15', sep = ''), 
           Currentmonth2 = paste(Currentmonth2, '-15', sep = '')) %>% 
    mutate(Currentmonth2 = as.Date(Currentmonth2), 
           Fiscal.Month2 = as.Date(Fiscal.Month2)) %>% 
    mutate(max_month = Currentmonth2 %m+% months(xmonths)) %>%  
    filter(Fiscal.Month2 <= max_month) %>% 
    select(Item, LC, Fiscal.Month, fcst)  


  # Item   LC Fiscal.Month fcst
  # 1 0S1576 MW92     2019-M06   22
  # 2 0S1576 MW92     2019-M06   18
  # 3 0S1576 RM11     2019-M06   12
  # 4 0S1576 MW92     2019-M07   10
  # 5 0S1576 RM11     2019-M07   10
  # 6 0S1576 MW92     2019-M08   12
  # 7 0S1576 MW92     2019-M09   10
  #   

【讨论】:

  • 如果我的输入如下所示,在所有财政月份都是 2019-M06 的输出中没有给我 RM11 怎么办
  • 我已经编辑了输入。您能否检查一下,因为它不适用于此。我想要即将到来的 x 个月的所有值,无论是多个财政月
  • @AnshulS 这个答案更健壮,请确保您运行代码并在每个步骤后查看输出
  • 这背后的逻辑是什么:filter(month_rank
  • 自那以后我已经编辑了答案,所以这个答案以月为单位,并施加了 3 个月的限制。旧的只是为每个月分配一个排名,从最新到最旧(按项目 + LC 分组后)。
猜你喜欢
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
相关资源
最近更新 更多