简体   繁体   English

如何在将年份设置为单独值的数据集中对十年进行的观察进行分组和计数?

[英]How can I group and count observations made by decade in a dataset that has the years set as individual values?

I have the folllowing dataset:我有以下数据集:

# A tibble: 90 × 2
   decade     n
    <dbl> <int>
 1   1930    13
 2   1931    48
 3   1932    44
 4   1933    76
 5   1934    73
 6   1935    63
 7   1936    54
 8   1937    51
 9   1938    41
10   1939    42
# … with 80 more rows

With more years that continue until 2010. I wish to "group" by decade like 1930s, 1940s, etc... and to have on another column the count of n in each year until the end of the decade.随着更多年份一直持续到 2010 年。我希望按 1930 年代、1940 年代等十年“分组”……并在另一列中统计十年结束前每年的 n 数。

For example:例如:

# A tibble: 90 × 2
   decade     n
    <dbl>        <int>
 1   1930-1939    449
 2   1940-1949    516 

Thanks!谢谢!

We could use the modulo operator %% :我们可以使用模运算符%%

library(dplyr)

df %>% 
  group_by(decade = decade - decade %% 10) %>% 
  summarise(n = sum(n))
  decade     n
   <dbl> <int>
1   1930   505

暂无
暂无

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

相关问题 我如何堆叠我的数据集,以便每个观察结果都与组内除自身之外的所有其他观察结果相关? - How can I stack my dataset so each observation relates to all other observations but itself, within a group? 如何重新计算 R 中具有不同观测值的个体值和平均值混合的平均值? - How can I recalculate the mean of a mixture of individual and mean values with varying numbers of observations in R? 包括在 count() 中没有观察到的年份 - Including years with no observations in count() 在因子*组组合没有观测值的箱线图中,如何保持箱宽一致? - How can I maintain consistent box width in a boxplot where factor*group combination has no observations? 如何检查每个组中数据的观察数是否相等? - How can I check whether data has an equal number of observations per group? 如何计算数据集中 NA 值的数量? - How can I count number of NA values in dataset? 从包含间隔数据的数据集中每年对观察进行分组和计数 - Group and count observations per year from a dataset containing interval data 如何将组均值与单个观察值进行比较并创建新的 TRUE/FALSE 列? - How do I compare group means to individual observations and make a new TRUE/FALSE column? 从R中的数据框中获取具有多个单独观察的组级观察计数 - Get count of group-level observations with multiple individual observations from dataframe in R 如何计算组内二元观察的数量? - How to count number of binary observations within a group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM