我很想知道为什么以下代码存在差异。其中mylist
是数据框列表和“try-error”类列表中的单个条目。如果列表仅填充数据框,则不会发生此问题。
mylist = split(iris, iris$Species)
mylist[['error-entry']] = try(errorplease)
Error in try(erlarorplease) : object 'errorplease' not found
然后当我运行lapply
lapply(mylist, FUN=function(x){summary(x$Species)}
返回以下错误:
Error in x$score : $ operator is invalid for atomic vectors
在阅读了一下之后,我发现我可以通过使用
来规避这个问题lapply(mylist, FUN=function(x){summary(x["Species"])})
但真正的问题是,如果我在功能上输入调试浏览器并询问x$Species
的摘要,它就可以了!
lapply(mylist, FUN=function(x){browser();summary(x$Species)})
Called from: FUN(X[[1L]], ...)
Browse[1]> summary(x$Species)
[1] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[11] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[21] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[31] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[41] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
我错过了哪些基本要素?是否在我访问浏览器时以不同的顺序遍历列表?在@Roland的帮助之后猜测这一点