检索月份数据

时间:2018-01-10 06:02:49

标签: r date dplyr

df<-data.frame(id=c("xx33","xx33","xx22","xx11","xx11","xx00"),amount=c(10,15,100,20,10,15),date=c("01/02/2013","01/02/2013","02/02/2013","03/03/2013","03/03/2013","04/04/2013"))

    id  amount  date
1   xx33    10  01/02/2013
2   xx33    15  01/02/2013
3   xx22    100 02/02/2013
4   xx11    20  03/03/2013
5   xx11    10  03/03/2013
6   xx00    15  04/04/2013

输出如

    id  201302  201303  201304
1   xx33    25    0        0
2   xx22   100    0        0
3   xx11    0     30       0
4   xx00    0     0        15 

3 个答案:

答案 0 :(得分:1)

tidyverse的另一种方式:

library(tidyverse)
library(lubridate)

df %>%
  mutate(month_year = format(dmy(date), "%Y%m")) %>%
  group_by(month_year, id) %>% 
  summarise(s = sum(amount)) %>% 
  spread(month_year, s, fill = 0)

# A tibble: 4 x 4
  id     `201302` `201303` `201304`
* <fctr>    <dbl>    <dbl>    <dbl>
1 xx00        0        0       15.0
2 xx11        0       30.0      0  
3 xx22      100        0        0  
4 xx33       25.0      0        0  

答案 1 :(得分:0)

转换&#39;日期&#39;到Date课程,提取“年份”&#39;使用format并使用xtabsbase R

中创建数据透视表
df$monthyear <- format(as.Date(df$date, "%d/%m/%Y"), "%Y%m")
xtabs(amount~ id + monthyear, df)
#       monthyear
#id     201302 201303 201304
#  xx00      0      0     15
#  xx11      0     30      0
#  xx22    100      0      0
#  xx33     25      0      0

答案 2 :(得分:0)

为了完整起见,这里也是一个使用dcast()包中的reshape2的解决方案,dplyr的前身之一:

library(reshape2)
library(lubridate)
dcast(df, id ~ format(dmy(date), "%Y%m"), sum, value.var = "amount")
    id 201302 201303 201304
1 xx00      0      0     15
2 xx11      0     30      0
3 xx22    100      0      0
4 xx33     25      0      0