I have a list of matrices in R and I want to extract only those elements (which are matrices here) whose all elements are NaN. I tried some functions from the "collapse" package namely gv and get_elem but I am not quite able to achieve what I want. Here is what I have done so far:
l1 <- list(matrix(NaN, nrow = 2, ncol = 2), matrix(1:4, nrow = 2, ncol = 2), matrix(NaN, nrow = 2, ncol = 2))
l2 <- get_elem(l1, function(x) is.nan(x) == TRUE)
I truly appreciate any help!
CodePudding user response:
You need function all
here. So try
l2 <- collapse::get_elem(l1, function(x) all(is.nan(x)))
Without additional packages, you can do either of the following:
l2 <- l1[sapply(l1, function(x) all(is.nan(x)))]
l2 <- Filter(function(x) all(is.nan(x)), l1)