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
答案 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
并使用xtabs
在base 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