可能很容易但很难获得。
如何按年份获取以下数据中低于112的值的百分比?输出为2个值,每年一个。
这是我尝试得出百分比的代码,但是它为每一行生成一个值,而不是对一年中的所有天进行汇总:
library(dplyr)
dat= a %>%
filter(value < 112) %>%
group_by(year) %>%
mutate(per=paste0(round(100*value/value,2),'%'))
a= structure(list(value = c(196, 186, 188, 186, 188, 191, 229, 277,
274, 258, 242, 261, 248, 226, 208, 196, 200, 255, 617, 515, 411,
328, 280, 267, 261, 251, 245, 236, 228, 220, 242, 345, 462, 515,
532, 549, 428, 348, 314, 300, 300, 320, 320, 303, 371, 566, 648,
609, 583, 555, 530, 476, 436, 402, 462, 589, 790, 736, 629, 623,
745, 1040, 1450, 1460, 1280, 1050, 878, 725, 648, 711, 779, 691,
623, 569, 487, 439, 445, 476, 535, 663, 711, 677, 629, 912, 1170,
994, 838, 810, 844, 790, 711, 629, 623, 564, 541, 555, 575, 697,
671, 756, 1030, 971, 799, 731, 697, 648, 617, 549, 513, 476,
433, 481, 784, 1320, 1150, 932, 736, 682, 657, 691, 617, 564,
535, 501, 515, 530, 481, 476, 530, 456, 391, 348, 334, 303, 297,
289, 286, 283, 289, 283, 279, 273, 267, 254, 245, 238, 238, 238,
232, 230, 230, 227, 227, 227, 227, 227, 227, 227, 220, 215, 210,
205, 205, 205, 205, 205, 200, 196, 191, 186, 181, 177, 172, 164,
160, 156, 156, 156, 154, 152, 152, 148, 144, 138, 133, 125, 114,
114, 111, 99.6999969482422, 89.5, 89.5, 88.5999984741211, 88.5999984741211,
56.5999984741211, 56.5999984741211, 56.5999984741211, 56.5999984741211,
56.5999984741211, 56.5999984741211, 56.5999984741211, 56.5999984741211,
56.5999984741211, 56.5999984741211, 56.5999984741211, 56.5999984741211,
56.5999984741211, 56.5999984741211, 56.5999984741211, 56.5999984741211,
56.5999984741211, 56.5999984741211, 56.5999984741211, 56.5999984741211,
48.7000007629395, 48.7000007629395, 48.7000007629395, 48.7000007629395,
48.7000007629395, 48.7000007629395, 48.7000007629395, 48.7000007629395,
48.7000007629395, 48.4000015258789, 48.0999984741211, 48.0999984741211,
48.0999984741211, 47, 47, 46.7000007629395, 46.4000015258789,
45.9000015258789, 45.2999992370605, 44.7000007629395, 44.2000007629395,
42.5, 41.9000015258789, 42.5, 46.7000007629395, 48.0999984741211,
48.0999984741211, 49.5999984741211, 47.2999992370605, 45.2999992370605,
39.0999984741211, 32.7999992370605, 32.7999992370605, 34, 34,
34.7999992370605, 36, 36, 36, 36, 36, 36, 36, 36, 35.4000015258789,
34.7999992370605, 34.7999992370605, 34.7999992370605, 34, 34,
34.7999992370605, 35.4000015258789, 36, 36, 36, 36.7999992370605,
34.7999992370605, 35.0999984741211, 36.7999992370605, 38.7999992370605,
38.7999992370605, 39.5999984741211, 39.5999984741211, 40.7999992370605,
40.7999992370605, 40.2000007629395, 39.5999984741211, 38.7999992370605,
38.5, 37.7000007629395, 37.7000007629395, 37.7000007629395, 37.7000007629395,
37.7000007629395, 37.7000007629395, 37.7000007629395, 37.7000007629395,
36.7999992370605, 36.7999992370605, 36.7999992370605, 36.7999992370605,
36.7999992370605, 36.7999992370605, 36.7999992370605, 36.7999992370605,
36.7999992370605, 36.7999992370605, 36.7999992370605, 36, 36,
34.7999992370605, 34, 34, 33.4000015258789, 32.7999992370605,
32, 32.5999984741211, 32.5999984741211, 32, 34, 34, 34, 33.4000015258789,
32.7999992370605, 32.5999984741211, 33.4000015258789, 33.4000015258789,
32.7999992370605, 32.7999992370605, 32, 31.1000003814697, 31.1000003814697,
31.1000003814697, 31.1000003814697, 30.6000003814697, 30, 32,
45.2999992370605, 56.5999984741211, 66, 68, 74.1999969482422,
79.9000015258789, 87.1999969482422, 99.0999984741211, 105, 102,
102, 108, 113, 120, 126, 135, 135, 135, 135, 135, 135, 135, 159,
179, 218, 187, 159, 139, 135, 135, 124, 128, 124, 124, 135, 135,
218, 278, 265, 278, 292, 385, 365, 385, 459, 436, 360, 306, 265,
265, 337, 345, 436, 385, 337, 385, 425, 365, 320, 311, 292, 311,
337, 436, 413, 354, 306, 278, 213, 246, 228, 208, 199, 189, 179,
175, 259, 306, 278, 278, 365, 484, 997, 968, 915, 835, 767, 753,
753, 767, 782, 782, 767, 767, 767, 699, 646, 538, 484, 425, 425,
459, 646, 1320, 2030, 2100, 1600, 1320, 1130, 1170, 1090, 1140,
968, 929, 835, 793, 835, 929, 875, 889, 1040, 1180, 1060, 954,
835, 699, 646, 606, 631, 646, 620, 631, 739, 767, 728, 592, 552,
566, 631, 552, 498, 436, 394, 413, 436, 1990, 1360, 1060, 900,
835, 807, 782, 807, 782, 915, 889, 875, 782, 674, 473, 552, 513,
513, 484, 513, 436, 413, 447, 425, 385, 566, 566, 271, 328, 306,
306, 283, 252, 228, 223, 218, 228, 234, 252, 218, 218, 208, 208,
228, 213, 208, 208, 203, 203, 213, 213, 189, 208, 240, 223, 223,
218, 218, 213, 208, 208, 208, 208, 213, 208, 203, 189, 189, 184,
179, 175, 171, 159, 163, 159, 146, 150, 154, 159, 159, 154, 146,
146, 119, 90.9000015258789, 63.0999984741211, 64.8000030517578,
64.8000030517578, 60.9000015258789, 62.9000015258789, 62.9000015258789,
62.9000015258789, 58.9000015258789, 58.9000015258789, 65.6999969482422,
63.7000007629395, 61.7000007629395, 60, 56.0999984741211, 56.0999984741211,
56.0999984741211, 49.2999992370605, 44.5, 42.5, 39.5999984741211,
39.5999984741211, 42.5, 41.5999984741211, 36.7999992370605, 36,
40.7999992370605, 45.5999984741211, 50.4000015258789, 55.2000007629395,
55.2000007629395, 56.0999984741211, 55.2000007629395, 50.4000015258789,
49.2999992370605, 48.4000015258789, 47.2999992370605, 50.4000015258789,
49.2999992370605, 52.0999984741211, 48.4000015258789, 48.4000015258789,
48.4000015258789, 48.4000015258789, 48.4000015258789, 49.2999992370605,
47.2999992370605, 48.4000015258789, 48.4000015258789, 48.4000015258789,
49.2999992370605), year = c(1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911,
1911, 1911, 1911, 1911, 1911, 1911, 1911, 1911, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912,
1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912, 1912
)), class = "data.frame", row.names = c(NA, -611L))
答案 0 :(得分:6)
一个有用的常见技巧是在值的逻辑向量上使用mean
以获取比例:
a %>%
group_by(year) %>%
summarise(per = scales::percent(mean(value < 112)))
## A tibble: 2 x 2
# year per
# <dbl> <chr>
#1 1911 23.3%
#2 1912 40.7%
答案 1 :(得分:1)
a %>%
group_by(year) %>%
summarise(per = 100*sum(value < 112)/n())
#> # A tibble: 2 x 2
#> year per
#> <dbl> <dbl>
#> 1 1911 23.3
#> 2 1912 40.7
答案 2 :(得分:1)
您需要使用summarise
来每年获得一行。
另外,不要一开始就过滤数据,而是创建一列,说明该值是否小于112
dat= a %>%
mutate(inf_112=(value < 112)) %>% # new column TRUE if value is below 112
group_by(year) %>%
summarise(pct=paste0(round(100*sum(inf_112)/n()),'%')) # divide the number or rows where value is below 112 by the total number of rows
答案 3 :(得分:0)
为完成起见,这是基数R和data.table
方法。
在基数R中,我们可以使用aggregate
aggregate(value~year, a, function(x) mean(x < 112) * 100)
# year value
#1 1911 23.26531
#2 1912 40.71038
或tapply
tapply(a$value, a$year, function(x) mean(x < 112) * 100)
在data.table
中可以完成
library(data.table)
setDT(a)[, (mean_val = mean(value < 112) * 100), by = year]