使用 summarise() 计算最小值重复的次数
Using summarise() to count the number of times the min value is repeated
我有这个 reach 数据框,其中包含有序值和可达性,我想要的输出是 table 几个属性的摘要 按集群 。整个 table 包含更多值,但我认为 10 行足以解释我想要实现的目标。
# A tibble: 500 x 3
Order Reachability Cluster
<int> <dbl> <dbl>
1 1 NA 1
2 2 1.54 1
3 3 1.54 1
4 4 0.860 1
5 5 0.821 1
6 6 0.821 1
7 7 0.821 1
8 8 0.821 1
9 9 0.821 1
10 10 0.821 1
# ... with 490 more rows
我创建了我的摘要 table,其中包含一些关于我的 reach table.
的职位信息
reach %>% dplyr::group_by(Cluster) %>%
summarise(first_value = first(na.omit(Reachability)),
min_value = min(na.omit(Reachability)),
last_value = last(na.omit(Reachability)),
first_pos = first(Order),
min_pos = Order[which.min(Reachability)],
last_pos = last(Order))
# A tibble: 1 x 7
Cluster first_value min_value last_value first_pos min_pos last_pos
<dbl> <dbl> <dbl> <dbl> <int> <int> <int>
1 1 1.54 0.821 0.821 1 5 10
我遇到的问题是 summarize 中的一个命令,它允许我计算“min_value”重复的次数。在这种情况下,对于 0.821
,“min_value”应该是 6。这是我尝试过但没有成功的方法:
... %>%
summarise(...
...
N_min = sum(Reachability == min(na.omit(Reachability))))
... %>%
summarise(...
...
N_min = count(min(na.omit(Reachability))))
我错过了什么吗?我真的不知道为什么我的第一个选择不起作用。据我了解,如果我按组执行该总和,应该给我一个满足我条件的 TRUE(或 1)的总和。谢谢!
数据:
reach <- structure(list(Order = 1:10, Reachability = c(NA, 1.53995982068778,
1.53995982068778, 0.860332791733694, 0.820585921380499, 0.820585921380499,
0.820585921380499, 0.820585921380499, 0.820585921380499, 0.820585921380499
), Cluster = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
您的第一个选项应该可以正常工作,但浮点数比较也不准确。 (参考 Why are these numbers not equal?)
在使用 sum
之前尝试将数字四舍五入。
summarise(
...
N_min = sum(round(Reachability, 2) == round(min(Reachability,na.rm = TRUE), 2))
...
)
我有这个 reach 数据框,其中包含有序值和可达性,我想要的输出是 table 几个属性的摘要 按集群 。整个 table 包含更多值,但我认为 10 行足以解释我想要实现的目标。
# A tibble: 500 x 3
Order Reachability Cluster
<int> <dbl> <dbl>
1 1 NA 1
2 2 1.54 1
3 3 1.54 1
4 4 0.860 1
5 5 0.821 1
6 6 0.821 1
7 7 0.821 1
8 8 0.821 1
9 9 0.821 1
10 10 0.821 1
# ... with 490 more rows
我创建了我的摘要 table,其中包含一些关于我的 reach table.
的职位信息reach %>% dplyr::group_by(Cluster) %>%
summarise(first_value = first(na.omit(Reachability)),
min_value = min(na.omit(Reachability)),
last_value = last(na.omit(Reachability)),
first_pos = first(Order),
min_pos = Order[which.min(Reachability)],
last_pos = last(Order))
# A tibble: 1 x 7
Cluster first_value min_value last_value first_pos min_pos last_pos
<dbl> <dbl> <dbl> <dbl> <int> <int> <int>
1 1 1.54 0.821 0.821 1 5 10
我遇到的问题是 summarize 中的一个命令,它允许我计算“min_value”重复的次数。在这种情况下,对于 0.821
,“min_value”应该是 6。这是我尝试过但没有成功的方法:
... %>%
summarise(...
...
N_min = sum(Reachability == min(na.omit(Reachability))))
... %>%
summarise(...
...
N_min = count(min(na.omit(Reachability))))
我错过了什么吗?我真的不知道为什么我的第一个选择不起作用。据我了解,如果我按组执行该总和,应该给我一个满足我条件的 TRUE(或 1)的总和。谢谢!
数据:
reach <- structure(list(Order = 1:10, Reachability = c(NA, 1.53995982068778,
1.53995982068778, 0.860332791733694, 0.820585921380499, 0.820585921380499,
0.820585921380499, 0.820585921380499, 0.820585921380499, 0.820585921380499
), Cluster = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
您的第一个选项应该可以正常工作,但浮点数比较也不准确。 (参考 Why are these numbers not equal?)
在使用 sum
之前尝试将数字四舍五入。
summarise(
...
N_min = sum(round(Reachability, 2) == round(min(Reachability,na.rm = TRUE), 2))
...
)