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:
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.
Any suggestion? Regards.
If K is a list of data frames this produces a new cleaned list of data.frames. The Inf2NA function replaces all infinite values in a vector v with NA.
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))
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.