Skip to content
Prev 323821 / 398503 Next

How to run lm for each subset of the data frame, and then aggreage the result?

On May 19, 2013, at 5:31 AM, CHEN, Cheng wrote:

            
This suggests you have used 'attach' on df. Not a safe practice.
That is exactly the sort of result I would have expected from your procedure. We cannot tell what you want that is different. For one thing you are posting  in HTML so the "aggregate result above is mangled.  I'm guessing it might have been.

countryname,  coeffname1      
USA         1.22      
GB          1.03     
France      1.1

So perhaps the only thing that is missing are the row names?

res <- do.call("rbind", lapply(df$countries, fitFunc)
rownames(res) <- as.character(df$countries)
res

If you had wanted a dataframe to be returned you could do this with the 'by' function or return a list with countries instead of a numeric vector from your 'fitFunc' calls. rbind a list of lists may give you something that should easily be coerced to data.frame. (But no data to test these theories)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^