【问题标题】:Aggregate data frame by date [duplicate]按日期聚合数据框[重复]
【发布时间】:2021-02-05 15:26:45
【问题描述】:
structure(list(ticker = c("WMT", "WMT", "WMT", "WMT", "WMT", 
"WMT"), filingdate = structure(c(18551, 18551, 18551, 18537, 
18536, 18534), class = "Date"), formtype = c("4", "4", "4", "4", 
"4", "4"), issuername = c("WALMART INC", "WALMART INC", "WALMART INC", 
"WALMART INC", "WALMART INC", "WALMART INC"), ownername = c("LORE MARC E", 
"LORE MARC E", "LORE MARC E", "BIGGS M BRETT", "LORE MARC E", 
"WALTON ALICE L"), officertitle = c("Executive Vice President", 
"Executive Vice President", "Executive Vice President", "Executive Vice President", 
"Executive Vice President", NA), isdirector = c("N", "N", "N", 
"N", "N", "N"), isofficer = c("Y", "Y", "Y", "Y", "Y", "N"), 
    istenpercentowner = c("N", "N", "N", "N", "N", "Y"), transactiondate = structure(c(18549, 
    18549, 18549, 18536, 18534, 18534), class = "Date"), securityadcode = c("ND", 
    "ND", "ND", "ND", "ND", "ND"), transactioncode = c("S", "S", 
    "S", "F", "F", "S"), sharesownedbeforetransaction = c(1669368, 
    1610399, 1591019, 302554, 1707160, 389404553), transactionshares = c(-58969, 
    -19380, -6651, -657, -37792, -606875), sharesownedfollowingtransaction = c(1610399, 
    1591019, 1584368, 301897, 1669368, 388797678), transactionpricepershare = c(144.114, 
    145.409, 145.891, 139.91, 137.25, 137.049), transactionvalue = c(8498258, 
    2818026, 970321, 91921, 5186952, 83171612), securitytitle = c("Common", 
    "Common", "Common", "Common Stock", "Common", "Common Stock"
    ), directorindirect = c("D", "D", "D", "D", "D", "I"), natureofownership = c(NA, 
    NA, NA, NA, NA, "By Trust"), dateexercisable = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"), 
    priceexercisable = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), expirationdate = structure(c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"), 
    rownum = c(1, 2, 3, 1, 1, 2), mycolumn = c(-8498258, -2818026, 
    -970321, -91921, -5186952, -83171612)), row.names = c(1L, 
2L, 3L, 9L, 13L, 15L), class = "data.frame")

我想压缩我的数据框以对“归档日期”列的每一天的“我的列”列中的所有值求和。 例如,2020-10-16 日重复 3 次。我希望新的 data.frame 有一行,日期为 2020-10-16,“我的列”列是其 3 个值的总和。 所有其他列都可以删除,也可以保留最后一天的值。

其他日期也是如此

【问题讨论】:

    标签: r dataframe aggregate


    【解决方案1】:

    一个选项可以是aggregate():

    #Code 1
    newdf <- aggregate(mycolumn~filingdate,data=df,sum,na.rm=T)
    

    输出:

      filingdate  mycolumn
    1 2020-09-29 -83171612
    2 2020-10-01  -5186952
    3 2020-10-02    -91921
    4 2020-10-16 -12286605
    

    或者dplyr:

    library(dplyr)
    #Code 2
    newdf <- df %>% group_by(filingdate) %>% summarise(mycolumn=sum(mycolumn,na.rm=T))
    

    输出:

    # A tibble: 4 x 2
      filingdate  mycolumn
      <date>         <dbl>
    1 2020-09-29 -83171612
    2 2020-10-01  -5186952
    3 2020-10-02    -91921
    4 2020-10-16 -12286605
    

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2020-07-23
      • 1970-01-01
      • 2020-05-12
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2021-03-08
      相关资源
      最近更新 更多