【问题标题】:How can I create a new column, based on date condition?如何根据日期条件创建新列?
【发布时间】:2022-11-12 03:44:56
【问题描述】:

我有两个数据框。 第一个遵循以下结构:

                   time transactID itemID order salesPrice        day
 1: 2018-01-01 00:01:56    2278968    450     1      17.42 2018-01-01
 2: 2018-01-01 00:01:56    2278968     83     1       5.19 2018-01-01
 3: 2018-01-01 00:07:11    2255797   7851     2      20.47 2018-01-01
 4: 2018-01-01 00:09:24    2278968    450     1      17.42 2018-01-01
 5: 2018-01-01 00:09:24    2278968     83     1       5.19 2018-01-01
 6: 2018-01-01 00:39:26    2257125   9375     1      31.02 2018-01-01
 7: 2018-01-01 00:51:59    2278968    450     1      17.42 2018-01-01
 8: 2018-01-01 00:51:59    2278968     83     1       5.19 2018-01-01
 9: 2018-01-01 00:51:59    2278968     19     1      77.64 2018-01-01
10: 2018-01-01 00:51:59    2278968    297     1      43.53 2018-01-01
...

第二个具有这种结构:

   day       
   <date>    
 1 NA        
 2 2018-07-13
 3 2018-07-01
 4 2018-07-02
 5 2018-07-05
 6 2018-07-11
 7 2018-07-04
 8 2018-07-08
 9 2018-07-09
10 2018-06-30
11 2018-07-06
12 2018-07-10
13 2018-07-03
14 2018-07-12
15 2018-07-07

我的目标是创建一个名为 prom 的新列,用于标记匹配的日期。例如,我在第一列和第二列之间有一个匹配项2018-07-13,那么它应该在当天的新列中标记,例如用星号 (*)。不匹配的行应该不填充""

我尝试的是这样的(没有工作,只是为了我想要完成的想法):

df1$prom <- ifelse(df1$day %in% df2$day, "*","")

复制代码(不包括匹配日期):

第一个df

structure(list(time = structure(c(1514764916, 1514764916, 1514765231, 
                                  1514765364, 1514765364, 1514767166, 1514767919, 1514767919, 1514767919, 
                                  1514767919, 1514767919, 1514767919, 1514768104, 1514768214, 1514768214, 
                                  1514768214, 1514768214, 1514768214, 1514768214, 1514770106), class = c("POSIXct", 
                                                                                                         "POSIXt"), tzone = "UTC"), transactID = c(2278968L, 2278968L, 
                                                                                                                                                   2255797L, 2278968L, 2278968L, 2257125L, 2278968L, 2278968L, 2278968L, 
                                                                                                                                                   2278968L, 2278968L, 2278968L, 2255111L, 2278968L, 2278968L, 2278968L, 
                                                                                                                                                   2278968L, 2278968L, 2278968L, 2255111L), itemID = c(450L, 83L, 
                                                                                                                                                                                                       7851L, 450L, 83L, 9375L, 450L, 83L, 19L, 297L, 295L, 109L, 2049L, 
                                                                                                                                                                                                       19L, 83L, 295L, 297L, 450L, 109L, 4322L), order = c(1L, 1L, 2L, 
                                                                                                                                                                                                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                           1L), salesPrice = c(17.42, 5.19, 20.47, 17.42, 5.19, 31.02, 17.42, 
                                                                                                                                                                                                                                                                               5.19, 77.64, 43.53, 37.79, 5.8, 35.75, 77.64, 5.19, 37.79, 43.53, 
                                                                                                                                                                                                                                                                               17.42, 5.8, 22.86), day = structure(c(17532, 17532, 17532, 17532, 
                                                                                                                                                                                                                                                                                                                     17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 
                                                                                                                                                                                                                                                                                                                     17532, 17532, 17532, 17532, 17532, 17532, 17532), class = "Date")), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                       -20L), class = c("data.table", "data.frame"))

第二个df

structure(list(day = structure(c(NA, 17725, 17713, 17714, 17717, 
                                 17723, 17716, 17720, 17721, 17712, 17718, 17722, 17715, 17724, 
                                 17719), class = "Date")), row.names = c(NA, -15L), class = c("tbl_df", 
                                                                                              "tbl", "data.frame"))

【问题讨论】:

    标签: r


    【解决方案1】:

    不确定这是否是您要查找的内容。你能告诉我们你期望的结果如何吗?

    df2$prom <- "*"
    
    library(tidyverse)
    left_join(df1, df2) %>%
      mutate(prom = replace_na(prom, ""))
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 2019-05-17
      • 2015-01-28
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      相关资源
      最近更新 更多