繁体   English   中英

用汇总值重新排列R中的数据帧

[英]Rearranging data frame in R with summarizing values

我需要重新排列一个数据帧,当前看起来像这样:

> counts
       year     score   freq rounded_year
    1: 1618         0     25         1620
    2: 1619         2      1         1620
    3: 1619         0     20         1620
    4: 1620         1      6         1620
    5: 1620         0     70         1620
   ---                                   
11570: 1994       107      1         1990
11571: 1994       101      2         1990
11572: 1994        10    194         1990
11573: 1994         1  30736         1990
11574: 1994         0 711064         1990

但是我需要的是每十年的score唯一值的计数( rounded_year )。 因此,数据框应如下所示:

rounded_year  0       1      2   3  [...] total
1620          115     6      1   0        122
---
1990          711064  30736  0   0        741997

我玩过aggregateddply ,但是到目前为止没有成功。 我希望,我的意思很清楚。 我不知道如何更好地描述它。

有任何想法吗?

一个使用dplyrtidyr简单示例。

dt = data.frame(year = c(1618,1619,1620,1994,1994,1994),
                score = c(0,1,0,2,2,3),
                freq = c(3,5,2,6,7,8),
                rounded_year = c(1620,1620,1620,1990,1990,1990))

dt

#    year score freq rounded_year
# 1 1618     0    3         1620
# 2 1619     1    5         1620
# 3 1620     0    2         1620
# 4 1994     2    6         1990
# 5 1994     2    7         1990
# 6 1994     3    8         1990


library(dplyr)
library(tidyr)

dt %>%
  group_by(rounded_year, score) %>%
  summarise(freq = sum(freq)) %>%
  mutate(total = sum(freq)) %>%
  spread(score,freq, fill=0) 


# Source: local data frame [2 x 6]
# 
#    rounded_year total     0     1     2     3
#           (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
# 1         1620    10     5     5     0     0
# 2         1990    21     0     0    13     8

如果您更喜欢使用data.table (因为您提供的数据集看起来更像data.table),则可以使用以下方法:

library(data.table)
library(tidyr)

dt = setDT(dt)[, .(freq = sum(freq)) ,by=c("rounded_year","score")]
dt = dt[, total:= sum(freq) ,by="rounded_year"]
dt = spread(dt,score,freq, fill=0)
dt

#    rounded_year total 0 1  2 3
# 1:         1620    10 5 5  0 0
# 2:         1990    21 0 0 13 8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM