how to subset Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘“function”’ to a data.frame