[英]Cleaning Inf values from an R list of dataframes
I've been working around with this response (that I think is a remarkable well constructed answer) but rather than a single dataframe I have a list of dataframes. 我一直在处理此响应 (我认为这是一个了不起的构造合理的答案),但是我没有一个数据框,而是有一个数据框列表。 So, I've been trying to do this:
因此,我一直在尝试这样做:
head(K[[1]])
a b c
1981 0.76 0.93 NaN
1982 Inf 0.33 NaN
1983 0.25 Inf NaN
1984 0.77 0.73 Inf
1985 Inf 0.85 Inf
1986 0.63 0.56 NaN
K <- lapply(seq_along(K), function(i) for (j in 1:3) set(K[[i]], which(is.infinite(K[[i]][[j]])), j, NA))
Which basically it's supposed to replace the Inf values in every element of the list K but I only get this result: 基本上应该替换列表K的每个元素中的Inf值,但我只能得到以下结果:
str(K)
List of 200
$ : NULL
$ : NULL
$ : NULL
$ : NULL
...
The for
loop inside the function works when isolated but I cannot get a list with the infinite-free dataframes. 孤立时该函数内部的
for
循环有效,但我无法获得包含无穷大数据帧的列表。
Any suggestion? 有什么建议吗? Regards.
问候。
If K is a list of data frames this produces a new cleaned list of data.frames. 如果K是数据帧列表,则将生成一个新的data.frames清除列表。 The Inf2NA function replaces all infinite values in a vector v with NA.
Inf2NA函数用NA替换向量v中的所有无限值。
Inf2NA <- function(v) replace(v, is.infinite(v), NA)
lapply(K, function(d) replace(d, TRUE, sapply(d, Inf2NA)))
If it's sufficient to create a list of matrices then this shorter version would be sufficient: 如果创建一个矩阵列表就足够了,那么这个简短的版本就足够了:
lapply(K, function(d) sapply(d, Inf2NA))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.