2

I am curious to understand why there is a difference in the following code. Where mylist is a list of data frames and a single entry in the list of the "try-error" class. This problem does not happen if the list is only filled with data frames.

mylist = split(iris, iris$Species)
mylist[['error-entry']] = try(errorplease)
Error in try(erlarorplease) : object 'errorplease' not found

Then when I run lapply

lapply(mylist, FUN=function(x){summary(x$Species)}

Which returns the following error:

Error in x$score : $ operator is invalid for atomic vectors

After reading a bit I've found that I can circumvent this issue by using

lapply(mylist, FUN=function(x){summary(x["Species"])})

But real question here is that if I enter to the debugging browser while on the function and the ask for the summary of x$Species, it works!

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

What fundamentals am I missing? Is it that when I access the browser I traverse the list in a different order? Guessing this after @Roland 's help

pedrosaurio
  • 4,708
  • 11
  • 39
  • 53

0 Answers0