简体   繁体   English

查找忽略-Inf和NA的R数据框列的最大值

[英]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.我有一个 R 数据帧,其中一些列有 -Inf 和 Na。 I would like to find the max of a specific column ignoring the Inf and NA.我想找到忽略 Inf 和 NA 的特定列的最大值。 My dataFrame df is as follow:我的dataFrame df如下:

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?我希望 my_max 等于 10。我该如何解决这个问题?

The function is.finite will identify elements in a (numeric) vector that are not in函数is.finite将识别(数字)向量中不在的元素

  • 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. hablar 包中有一个简单的解决方案。 BY adding s() before max you avoid this problem.通过在 max 之前添加 s() 可以避免这个问题。

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.返回 10 并忽略向量的 Inf 和 NA。

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:我从包含空白字段的 .csv 文件中读取时使用的一种方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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