how to compute sum of highest N values in a specific column/row

how to compute the sum of the highest N values in a specific column.

Thanks in advance.

var1
118759
67507.3
12094.7
4982.9
0
344237.3
330775.7
20419.4
72776.2
4668
31158.1
8139.5
0
15375.5
311206.4
219491.7
3553.3
7251.2
69050.8
9140.8
82464.8
8345.6
43671
576.4
22135.3
9922.9
11572.7
445775.9
0
160472.5
169887.4
18853.8
34719.9
22794
15289
16302.1
11107.8
0
5259.7
67214.7
314854.8
0
4323.1
20551.1
66419.9
81448
1444698.5
31189.3
47423.6
0
0
66458
140736.7
205156.7
113076.5
23041.3
77249.4
0

2 Likes

Given you have a single vector, is it not enough?

# x is the vector containing values
# N is the number of values
sum(tail(sort(x),N))
1 Like

library(dplyr)

var1 <- c( 118759, 67507.3, 12094.7, 4982.9, 0, 344237.3, 330775.7, 20419.4, 72776.2, 4668, 31158.1, 8139.5, 0, 15375.5, 311206.4, 219491.7, 3553.3, 7251.2, 69050.8, 9140.8, 82464.8, 8345.6, 43671, 576.4, 22135.3, 9922.9, 11572.7, 445775.9, 0, 160472.5, 169887.4, 18853.8, 34719.9, 22794, 15289, 16302.1, 11107.8, 0, 5259.7, 67214.7, 314854.8, 0, 4323.1, 20551.1, 66419.9, 81448, 1444698.5, 31189.3, 47423.6, 0, 0, 66458, 140736.7, 205156.7, 113076.5, 23041.3, 77249.4, 0)

var1 %>% sort(decreasing=TRUE) %>% sum(.[1:10])
[1] 9380137

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.