簡體   English   中英

R:每月匯總行數

[英]R: Summarize rows per month

我制作了一個 dataframe ,其中有一列包含日期,列包含數值。 我希望這個 dataframe 按月對自身進行分組,並對每個相應月份其他列的所有數值進行匯總。

這是我的 dataframe 示例:

capture.date  Test1  Test2  Test3
2016-03-18      0      1      1
2016-03-18      1      1      1
2016-03-20      2      1      1
2016-04-12      1      0      1

我已經嘗試了一些代碼:

df %>% 
  group_by(capture.date) %>% 
  summarise_each(funs(sum))

和:

aggregate(df[2:4], by=df["capture.date"], sum)

但是這兩個選項都返回按日而不是按月匯總的數據框。 我怎樣才能讓它按月而不是按天匯總?

所需的 output:

capture.date  Test1  Test2  Test3
2016-03         3      3      3     
2016-04         1      0      1

以下應該工作

library(lubridate)
library(tidyverse)

txt <- "capture.date  Test1  Test2  Test3
2016-03-18      0      1      1
2016-03-18      1      1      1
2016-03-20      2      1      1
2016-04-12      1      0      1"

data <- read.table(text = txt, header = TRUE)

data %>% 
  mutate(month = month(capture.date), 
         year = year(capture.date)) %>% 
  group_by(month, year) %>% 
  summarise_if(is.integer, sum) %>%
  ungroup %>%
  mutate("capture.date" = paste(year, str_pad(month, 2, side = "left", pad = "0"), sep = "-")) %>%
  select(capture.date, Test1, Test2, Test3)

這將產生

# A tibble: 2 x 4
  capture.date Test1 Test2 Test3
  <chr>        <int> <int> <int>
1 2016-03          3     3     3
2 2016-04          1     0     1

對於您的真實數據,您可能需要將 summarise_if 中的summarise_if更改為is.integer以外的其他內容。

1) dplyr/zoo使用末尾注釋中可重復顯示的數據將每個日期轉換為 yearmon class 代表沒有日期的日期,然后匯總數字列:

library(dplyr)
library(zoo)

df %>% 
  group_by(yearmon = as.yearmon(capture.date)) %>% 
  summarize_if(is.numeric, sum) %>%
  ungroup

給這個小標題:

# A tibble: 2 x 4
  yearmon   Test1 Test2 Test3
  <yearmon> <int> <int> <int>
1 Mar 2016      3     3     3
2 Apr 2016      1     0     1

2) zoo這可以在一個read.zoo命令中交替完成。 如果你想要一個 data.frame 作為結果, fortify.zoo可以用於結果:

library(zoo)
read.zoo(df, FUN = as.yearmon, aggregate = sum)

給這個動物園系列:

         Test1 Test2 Test3
Mar 2016     3     3     3
Apr 2016     1     0     1

2a) 帶有 magrittr 管道的動物園這也可以寫成帶有 magrittr(或 dplyr)管道的管道:

library(magrittr)
library(zoo)

df %>% read.zoo(FUN = as.yearmon, aggregate = sum)

或轉換為 data.frame

library(magrittr)
library(zoo)

df %>% read.zoo(FUN = as.yearmon, aggregate = sum) %>% fortify.zoo

3) Base R僅使用 Base R 提取每個日期的前 7 個字符,然后對其進行聚合:

df2 <- transform(df, year.month = substr(capture.date, 1, 7), capture.date = NULL)
aggregate(. ~ year.month, df2, sum)

給出這個data.frame:

  year.month Test1 Test2 Test3
1    2016-03     3     3     3
2    2016-04     1     0     1

筆記

可重現形式的輸入:

Lines <- "
capture.date  Test1  Test2  Test3
2016-03-18      0      1      1
2016-03-18      1      1      1
2016-03-20      2      1      1
2016-04-12      1      0      1"
df <- read.table(text = Lines, header = TRUE, as.is = TRUE)

您可以在group_by()中將日期提取為%Y-%m格式,並使用summarise_if()或 summarise_at summarise_at()到 select 對哪些變量求和。

(確認capture.dateDate類)

df %>%
  group_by(Date = strftime(capture.date, "%Y-%m")) %>%
  summarise_if(is.numeric, sum)

# # A tibble: 2 x 4
#   Date    Test1 Test2 Test3
#   <chr>   <int> <int> <int>
# 1 2016-03     3     3     3
# 2 2016-04     1     0     1

使用自定義 function:

df %>% group_by(x) %>% 
summarize_each(function(i)if(is.numeric(i)){sum(i)}else{unique(i)})

或者你需要總結列。 對矩陣求和只是總結一切

res = do.call(rbind,by(df[2:4],df["capture.date"],colSums))
res = data.frame(capture.date=rownames(res),res)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM