简体   繁体   中英

Finding the max of a R dataframe column ignoring -Inf and NA

I have a R dataFrame from which some columns have -Inf and Na. I would like to find the max of a specific column ignoring the Inf and NA. My dataFrame df is as follow:

column1     column2
  -Inf        2
   4          8
   Na         5
   7          4
   10         4 

I tried using

temp=df
temp[is.infinite(temp)]<-NA
my_max=max(temp$column1, na.rm=TRUE)

but I get the following error:

Error in is.infinite(temp) : default method not implemented for type 'list'

I would like to my_max to be equal to 10. How can I tackle this problem?

The function is.finite will identify elements in a (numeric) vector that are not in

  • NA
  • NaN
  • Inf
  • -Inf

Thus, this function can subset your column of interest in one step.

temp <- read.table(text = "
  column1     column2
  -Inf        2
   4          8
   NA         5
   7          4
   10         4",
   header = TRUE)

max(temp$column1[is.finite(temp$column1)])
# [1] 10

There is a simple solution in the hablar package. BY adding s() before max you avoid this problem.

data <- data.frame(column1 = c(-Inf, 4, NA, 7, 10), column2 = c(2, 8, 5, 4, 4))

max(s(data$column1))

Return 10 and have ignores the Inf and NA of the vector.

One solution would be the following:

data <- data.frame(column1 = c(-Inf, 4, NA, 7, 10), column2 = c(2, 8, 5, 4, 4))
column1b <- data$column1[which(!is.na(data$column1))]
column1c <- column1b[which(column1b < Inf)]
max(column1c)

A method I used when reading from a .csv file that contained blank fields:

df[df==""] <- NA
df <- na.omit(df)
print(max(df[,1]))

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