Using Stargazer in R to export csv direct into Excel -
i trying stargazer
r package spit out huge .csv. descriptive statistics table 43 different companies excel.
i have 43 different data frames pertaining meetings @ 43 different corporation. trying create output each 1 of these data frames list mean value of variable of meetings, standard deviation of variable august, max, min, , total number. reproducible example below company 1 "j" standing july, "a" august, "s" september, , "n" november:
j s n 29 2 30 2 20 3 30 10 30 10 20 30 40 20 10 21 10 30 13 14
company 2
j s n 14 5 15 19 10 10 17 10 30 14 18 11 10 20 19 9 10 10 20 4
the following code used:
stargazer(company1[c("j", "a", "s", "n")], type = "text", title="descriptive statistics company 1", digits=1, out="table1.txt", covariate.labels= c("j", "a", "s", "n")) stargazer(company2[c("j", "a", "s", "n")], type = "text", title="descriptive statistics augustmartin", digits=1, out="table1.txt", covariate.labels= c("j", "a", "s", "n")) ... stargazer(company43[c("j", "a", "s", "n")], type = "text", title="descriptive statistics augustmartin", digits=1, out="table1.txt", covariate.labels= c("j", "a", "s", "n"))
i trying stargazer package take descriptive statistics company 1 company 43, preserve headings know descriptive statistics belong company , dump them convenient csv sheet in excel.
when try use following code
library(xlsx) # create single summary table , write excel file tab1 = t(sapply(company1, function(x) { data.frame(n=length(x), mean=mean(x), `st. dev.`=sd(x), min=min(x), max=max(x)) })) write.xlsx(tab1, "company data.xlsx", sheetname="company1", row.names=false)
i following:
col 1 col2 col3 col4 51.34 #n/a 51.34 51.34 7.58 #n/a 7.58 7.58 26.99 #n/a 26.99 26.99 49.74 #n/a 49.74 49.74 34.84 #n/a 34.84 34.84 54.84 #n/a 54.84 54.84 18.5 #n/a 18.5 18.5 49.34 #n/a 49.34 49.34 49.26 #n/a 49.26 49.26
it taking mean of each row , not mean of specified column. ideally add in col 1, find mean, standard deviation, min , max.
stargazer
doesn't seem right tool this. instead, create data frame summary data want , write excel file. below provide examples single data frame , bunch of data frames.
save summary of single data frame
library(xlsx) # create single summary table , write excel file tab1 = t(sapply(company1, function(x) { data.frame(n=length(x), mean=mean(x), `st. dev.`=sd(x), min=min(x), max=max(x)) })) write.xlsx(tab1, "company data.xlsx", sheetname="company1", row.names=false)
save summaries of multiple data frames
here's how write bunch of summary tables file. ideally, read data frames list , operate on each element of list. way won't have repeat same code each individual data frame.
to create list of data frames, read in data this:
# names of files read file.names = list.files(pattern="company.*csv") df.list = sapply(file.names, read.csv)
for example, i'll create list of data frames using sample data company1
, company2
provided:
df.list = list(company1=company1, company2=company2) # calculate summary statistics each data frame , write excel worksheet sapply(names(df.list), function(df) { tab1 = t(sapply(df.list[[df]], function(x) { data.frame(n=length(x), mean=mean(x), `st. dev.`=sd(x), min=min(x), max=max(x)) })) write.xlsx(tab1, "company data.xlsx", sheetname=df, row.names=false, append=true) })
Comments
Post a Comment