如何在R中按月份组合多个数据框

问题描述 投票:6回答:2

我在下面提到了不同的数据帧:

DF1:

Origination_Date        Count1        Count2
2018-07-01              147           205
2018-07-05              180           345
2018-07-08              195           247
2018-08-04              205           788

DF2:

Date              ID
2018-07-01        I-1
2018-07-02        I-2
2018-07-02        I-3
2018-07-03        I-4
2018-07-03        I-5
2018-08-04        I-6
2018-08-04        I-7

支付

Create_Date           ID
2018-07-01            I-1
2018-07-02            I-2
2018-07-03            I-3
2018-08-04            I-4
2018-08-04            I-5

通过利用上述多个数据帧,我想通过MonthYear创建一个新的数据帧组,并在月份和日期方面表示合并计数,如下面的示例数据框所示。

要求输出:

Month   Count1   Count2   DF2_Count(ID)    DF3_Count(ID)
Aug-18  205      788      2                2
Jul-18  522      797      5                3
Jun-18  0        0        0                0

上面提到的相同数据结构也希望在日期的基础上创建,我尝试使用group_by函数,并且可以为每个单独的datafreme创建所需的数据帧,但不能通过合并所有数据帧。

注意: - 虽然我的数据框中没有Jun-18月,但我想在同一个月创建一行(想要在最近一个月创建至少三个月的所需输出数据帧(即如果它的Sep-18Aug-18Jul-18 ) - 如果任何数据帧的0行比显示计数0,则为必需输出。

r dataframe dplyr
2个回答
1
投票

这样的事情怎么样:

# your data
df1 <- data.frame (Origination_Date = c('2018-07-01','2018-07-05','2018-07-08','2018-08-04'),
                   Count1 = c(147,180,195,205), Count2 = c(205,345,247,788))
df2 <- data.frame (Date = c('2018-07-01','2018-07-02','2018-07-02','2018-07-03','2018-07-03','2018-08-04','2018-08-04'),
                   ID = c('I-1','I-2','I-3','I-4','I-5','I-6','I-7'))
df3 <- data.frame (Create_Date = c('2018-07-01','2018-07-02','2018-07-03','2018-08-04','2018-08-04'), ID = c('I-1','I-2','I-3','I-4','I-5'))

# package to manage date
library(lubridate)

# first we create the yyyy-mm data.frame grouped
df1_1 <- df1 %>% 
       mutate(ym = format(ymd(Origination_Date),'%Y-%b')) %>%
       group_by(ym) %>%
       summarise(Count1 = sum(Count1) ,Count2 = sum(Count2))

df2_1 <- df2 %>%
      mutate(ym = format(ymd(Date),'%Y-%b')) %>%
      group_by(ym) %>%
      summarise(DF2_Count = n())

df3_1 <- df3 %>%
        mutate(ym = format(ymd(Create_Date),'%Y-%b')) %>%
        group_by(ym) %>%
       summarise(DF3_Count = n())


# join them together
df_1 <- df1_1 %>% full_join(df2_1, by = 'ym') %>% full_join(df3_1, by = 'ym')

    > df_1
# A tibble: 2 x 5
  ym       Count1 Count2 DF2_Count DF3_Count
  <chr>     <dbl>  <dbl>     <int>     <int>
1 2018-Aug    205    788         2         2
2 2018-Jul    522    797         5         3

现在是棘手的部分,添加缺少的月份,我创建了一对如果谁检查是否没有最大月 - 2 - (第二个),它添加一个假行,第一个为最后一个但是一个。

if(
  format(floor_date(as.Date(max(union(union(df1[,1], df2[,1]),df3[,1]))), "month") - months(1),'%Y-%b') %in% df_1$ym == F){
  df_2 <- data.frame(ym =format(floor_date(as.Date(max(union(union(df1[,1], df2[,1]),df3[,1]))), "month") - months(1),'%Y-%b'),
                     Count1 = 0,
                     Count2 = 0,
                     DF2_Count= 0,
                     DF3_Count= 0)
  rbind(df_1,df_2)} else {'it already exists'}
[1] "it already exists"


if(
format(floor_date(as.Date(max(union(union(df1[,1], df2[,1]),df3[,1]))), "month") - months(2),'%Y-%b') %in% df_1$ym == F){
df_2 <- data.frame(ym =format(floor_date(as.Date(max(union(union(df1[,1], df2[,1]),df3[,1]))), "month") - months(2),'%Y-%b'),
                   Count1 = 0,
                   Count2 = 0,
                   DF2_Count= 0,
                   DF3_Count= 0)
rbind(df_1,df_2)
} else {'it already exists'}

    # A tibble: 3 x 5
      ym       Count1 Count2 DF2_Count DF3_Count
      <chr>     <dbl>  <dbl>     <dbl>     <dbl>
    1 2018-Aug    205    788         2         2
    2 2018-Jul    522    797         5         3
    3 2018-Jun      0      0         0         0

0
投票

这是data.table的解决方案:

library(data.table)

DF1 <- fread(
"Origination_Date        Count1        Count2
2018-07-01              147           205
2018-07-05              180           345
2018-07-08              195           247
2018-08-04              205           788")

DF2 <- fread(
"Date              ID
2018-07-01        I-1
2018-07-02        I-2
2018-07-02        I-3
2018-07-03        I-4
2018-07-03        I-5
2018-08-04        I-6
2018-08-04        I-7")

DF3 <- fread(
"Create_Date           ID
2018-07-01            I-1
2018-07-02            I-2
2018-07-03            I-3
2018-08-04            I-4
2018-08-04            I-5")

S1 <- DF1[, Ymon:=substr(Origination_Date, 1, 7)][, .(sum(Count1), sum(Count2)), Ymon]
S2 <- DF2[, Ymon:=substr(Date, 1, 7)][, .(DF2count=.N), Ymon]
S3 <- DF3[, Ymon:=substr(Create_Date, 1, 7)][, .(DF3count=.N), Ymon]

S <- merge(data.table(Ymon=paste0("2018-0", 6:8)), S1, all.x=TRUE)
S <- merge(S, S2, all.x=TRUE)
S <- merge(S, S3, all.x=TRUE)
S
# > S
#       Ymon  V1  V2 DF2count DF3count
# 1: 2018-06  NA  NA       NA       NA
# 2: 2018-07 522 797        5        3
# 3: 2018-08 205 788        2        2

如果你想要0而不是NA,你可以这样做:

S[is.na(S)] <- 0
S
#       Ymon  V1  V2 DF2count DF3count
# 1: 2018-06   0   0        0        0
# 2: 2018-07 522 797        5        3
# 3: 2018-08 205 788        2        2
© www.soinside.com 2019 - 2024. All rights reserved.