You need to use lapply
instead of sapply
.
sapply
simplifies too much. Try:
lapply(ListOfData, function(X) lm(y~x, X))
sapply(ListOfData, function(X) lm(y~x, X))
I don't know exactly the distinction, but if you're ever expect the output of each item of sapply
to have extractable parts (i.e. Item$SubItem
), you should use lapply
instead.
Update
Answering your next question, you can do either:
names(models) <- h1_lines
names(h1_lines) <- h1_lines ## Before lapply
And call them by
models[["65"]]
Remember to use quotes around the numbers. As a side note, naming list items with numbers is not always the best idea. A workaround could be:
models[[which(h1_lines==65)]]