假设我在R中有一个非常大的列表,有些值是有效的,有些是无效的。该示例使用的列表只有6个元素。
library(purrr)
library(dplyr)
myList <- list(-1, 0, 1, 2, 'poo', 'hi')
safe_log <- safely(log)
results <- myList %>%
map(safe_log) %>%
transpose()
allErrors <- results[['error']]我有三个问题:
allErrors,myList的哪些元素无效?我在寻找一个整数向量,它返回:[1] 5 6allError只显示错误消息。我期望这个输出:[[5]]
<simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>
[[6]]
<simpleError in .Primitive("log")(x, base): non-numeric argument to mathematical function>myList仅适用于有效值。我想要一个看起来像这样的新名单:[[1]]
[1] -1
[[2]]
[1] 0
[[3]]
[1] 1
[[4]]
[1] 2发布于 2019-07-25 01:30:28
inherits检查哪些元素包含某种错误:
is_error = allErrors %>% map_lgl(~ inherits(.,"error"))https://stackoverflow.com/questions/57192955
复制相似问题