obtain the smallest value of a column in relation to the maximum value of another column

StackOverflow https://stackoverflow.com/questions/23371655

  •  12-07-2023
  •  | 
  •  

Pergunta

I have a question regarding the handling of values ​​in a data.frame. The following data.frame would get the value of the first column that are in the same row as the maximum value of the second column. In case there are several equal maximum values ​​in the second column, I would like to return the smallest value of them in the first column

    power   total   found
1   31.0    7   7
2   30.5    8   7
3   30.0    9   8
4   29.5    9   8
5   29.0    9   7
6   28.5    8   7
7   28.0    10  8
8   27.5    10  8
9   27.0    10  8
10  26.5    11  9
11  26.0    11  9
12  25.5    9   8
13  25.0    9   9
14  24.5    8   8
15  24.0    10  9
16  23.5    8   7
17  23.0    9   8
18  22.5    7   7
19  22.0    7   7
20  21.5    7   7
21  21.0    4   4
22  20.5    4   4
23  20.0    3   3
24  19.5    2   2
25  19.0    2   2
26  18.5    2   2
27  18.0    2   2
28  17.5    1   1
29  17.0    1   1
30  16.5    0   0
31  16.0    0   0
32  15.5    0   0
33  15.0    0   0
Foi útil?

Solução

# Create some fake data
set.seed(14)
df = data.frame(power = sample(seq(15,31,0.5),30, replace=TRUE), 
     total= sample(c(0,1,2,3,7:11), 30, replace=TRUE), 
     found=sample(c(0:2,7:9), 30, replace=TRUE))

df$total[c(5,9)] = NA  # Add some missing data

# Minimum of `power` at maximum of `total`
min(df$power[df$total==max(df$total, na.rm=TRUE)], na.rm=TRUE)
[1] 17.5

If you want to see all the values of power at the maximum value of total:

df$power[df$total==max(df$total, na.rm=TRUE)]
[1]   NA   NA 17.5 25.0 30.5 31.0

You can use the which function if you don't want missing values returned:

df$power[which(df$total==max(df$total, na.rm=TRUE))]
[1] 17.5 25.0 30.5 31.0

which returns only the row indices for which the logical test is TRUE, while the logical test alone returns a value of TRUE, FALSE, or NA for every single row.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top