简体   繁体   中英

Find max value in a column that contain -Inf and NA values while using dplyr and tidyR functions

I have a data frame that looks like this and I want to find the max value in the col1 using dplyr functions such as slice_n and top_n

df = data.frame(col1=c(-Inf, 10,NaN, NA,200,Inf), col2=c(30,30, 10,200,20,10))
 col1 col2
  -Inf   30
    10   30
   NaN   10
    NA  200
   200   20
   Inf   10

So far I have not managed this and I was wondering if the community could help me or show me a tip. I highly appreciate your time

To find the maximum non-infinite value of in col1 :

df %>% 
  filter(!is.infinite(col1)) %>% 
  summarise(Max=max(col1, na.rm=TRUE)) %>%
  pull(Max)

[1] 200

Or

max(df$col1[!is.infinite(df$col1)], na.rm=TRUE)
[1] 200

To find the row containing the maximum non-infinite value in col1 :

df %>% filter(!is.infinite(col1)) %>% slice_max(col1) 
  col1 col2
1  200   20

The calls to filter() are in response to OP's indication that they require a non-infinte value to be returned. If infinite values are are acceptable, simply omit the calls. Note that slice_max et al, as requested by OP in their question, do not return values from the column but rather rows from the data frame .

Note that top_n() is superseded.

df %>% gather() %>% group_by(key) %>% slice_max(value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM