如何在R

时间:2018-09-17 08:22:07

标签: r dataframe dplyr

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

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

DF3

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创建所需的数据框,但不能通过合并所有数据框来创建。

注意: -尽管我的datframe中没有Jun-18个月,但我想为   同一个月(希望在所需的输出中至少创建三个月)   考虑最近一个月的数据框(即,如果Sep-18Aug-18大,并且   Jul-18)。 -如果任何数据框的行数超过显示的行数0,则必须输出0。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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