Stataq
Stataq

Reputation: 2307

how to get summary table with by () into a table or data.frame

I tried to get a summary stat of a variable Change_1. I can get what I want by using codes by( df3.1_P2.1$Change_1, df3.1_P2.1$AGEGROUP_CLA, summary ).

The output is looks like following:

enter image description here

I would like to somehow to get those info into a table which I can export to an excel file. what should I do? Maybe by() is not the best way to do it. Any suggestion?

Upvotes: 1

Views: 93

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

You can rbind the output from by to get a dataframe which can be exported to excel.

res <- data.frame(do.call(rbind, by(mtcars$mpg, mtcars$cyl, summary)))
res

#  Min. X1st.Qu. Median     Mean X3rd.Qu. Max.
#4 21.4    22.80   26.0 26.66364    30.40 33.9
#6 17.8    18.65   19.7 19.74286    21.00 21.4
#8 10.4    14.40   15.2 15.10000    16.25 19.2

writexl::write_xlsx(res, 'data.xlsx')

Upvotes: 1

Related Questions