I want to use the stargazer package to have a ready to publish summary statics for each subgroup of crops for a table like this:
crop | yield |
---|---|
corn | xx |
wheat | yy |
soybean | zz |
I have already seen this post , but it was not exactly what I was looking for.
Can someone help me with this?
What exactly do you not like from the suggested solution? Please specify your data and the output format. For grouped descriptive summary statistics, I would opt for gtsummary
package. The main difference is the output format. stargazer
is good at text and latex, gtsummary produces great html output.
# Agricultural data on crops and yield
library(agridat)
data("bose.multi.uniformity")
table(bose.multi.uniformity$crop)
# Grouped descriptives
# tidyverse and stargazer output, i.e. text or latex
library(tidyverse)
library(stargazer)
bose.multi.uniformity %>%
group_by(crop) %>%
mutate(id = 1:n()) %>%
ungroup() %>%
gather(temp, val, yield) %>%
unite(temp1, crop, temp, sep = '_') %>%
select(-c(year, row, col)) %>%
spread(temp1, val) %>%
select(-id) %>%
as.data.frame() %>%
stargazer(type = 'text')
===========================================
Statistic N Mean St. Dev. Min Max
-------------------------------------------
barley_yield 390 634.244 161.091 180 1,040
lentil_yield 390 215.713 63.464 81 431
wheat_yield 390 251.838 71.824 70 450
-------------------------------------------
# tidyverse and tbl summary, i.e. html
library(gtsummary)
bose.multi.uniformity %>%
select(crop, yield) %>%
tbl_summary(statistic = list(all_continuous() ~ "{mean} ({min}, {max})"), by = crop) %>%
as_gt()
Best
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.