LDT
LDT

Reputation: 3088

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

Upvotes: 0

Views: 1120

Answers (2)

Limey
Limey

Reputation: 12451

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.

Upvotes: 1

nadjib amrouche
nadjib amrouche

Reputation: 41

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

Upvotes: 0

Related Questions