![](/img/trans.png)
[英]calculate the mean for a column of a dataframe in R ignoring NA and 0 values
[英]Finding the max of a R dataframe column ignoring -Inf and NA
我有一个 R 数据帧,其中一些列有 -Inf 和 Na。 我想找到忽略 Inf 和 NA 的特定列的最大值。 我的dataFrame df如下:
column1 column2
-Inf 2
4 8
Na 5
7 4
10 4
我尝试使用
temp=df
temp[is.infinite(temp)]<-NA
my_max=max(temp$column1, na.rm=TRUE)
但我收到以下错误:
Error in is.infinite(temp) : default method not implemented for type 'list'
我希望 my_max 等于 10。我该如何解决这个问题?
函数is.finite
将识别(数字)向量中不在的元素
NA
NaN
Inf
-Inf
因此,此功能可以一步对您感兴趣的列进行子集化。
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
hablar 包中有一个简单的解决方案。 通过在 max 之前添加 s() 可以避免这个问题。
data <- data.frame(column1 = c(-Inf, 4, NA, 7, 10), column2 = c(2, 8, 5, 4, 4))
max(s(data$column1))
返回 10 并忽略向量的 Inf 和 NA。
一种解决方案如下:
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)
我从包含空白字段的 .csv 文件中读取时使用的一种方法:
df[df==""] <- NA
df <- na.omit(df)
print(max(df[,1]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.