snetlogo
snetlogo

Reputation: 59

How to save a table from table1 package into a CSV file

I am trying to figure out how to save tables developed with the package table1 into an excel file or into a picture.

In the website it is mentioned that "The table can be exported to a CSV with export = "file_name"" but when I try to do so, nothing is saved.

I have also noticed someone posted a question asking on how to save this table as an image but there was no reply.

Any idea how to solve this issue?

Thanks a lot in advance.

Upvotes: 2

Views: 2850

Answers (1)

freddyalfredo91
freddyalfredo91

Reputation: 56

The table1 package has the as.data.frame(x) function. So once you have your table1 output how you want it, define it as an object.

    `dat <- expand.grid(id=1:10, sex=c("Male", "Female"), treat=c("Treated", "Placebo"))
dat$age <- runif(nrow(dat), 10, 50)
dat$age[3] <- NA  # Add a missing value
dat$wt <- exp(rnorm(nrow(dat), log(70), 0.2))

label(dat$sex) <- "Sex"
label(dat$age) <- "Age"
label(dat$treat) <- "Treatment Group"
label(dat$wt) <- "Weight"

units(dat$age) <- "years"
units(dat$wt) <- "kg"

# basic table 1
tab1_html<- table1(~ sex + age + wt | treat, data=dat)
tab1_df <- as.data.frame(tab1html)`   

Which before you would give you:

Normal Table1 output

And now should give you

data frame version of table1

Upvotes: 3

Related Questions