Home > Back-end >  Saving descriptives table
Saving descriptives table

Time:02-18

I'm wondering if there is an easy way to save a descriptives table so I can input it into a word document? If I just copy and paste the table it messes up the formatting.

I'm running:

descriptives(data, 
         vars = vars(Mean1, Mean2, Mean3, Mean4),
         sd = TRUE,
         hist = TRUE)

is there a simple way of saving this table as an image or something I can add to word?

CodePudding user response:

One option is to save the output as .txt:

des <-   descriptives(data, 
         vars = vars(Mean1, Mean2, Mean3, Mean4),
         sd = TRUE,
         hist = TRUE)

capture.output(des, file = "tests.txt", append = TRUE)


CodePudding user response:

I find that saving the data to Excel, then copy-pasting from Excel to Word, generally works much better than directly pasting to Word. I end up doing this a lot, so I wrote a function to quickly open a data.frame in Excel:

# Saves dataframe as .csv in R temp directory, then opens in Excel (or other 
# system default for .csv). The .csv  will have a randomly-generated name 
# unless otherwise specified in `name`.

in_excel <- function (df, name, na = "") {
  csv_dir <- file.path(tempdir(), "csv")
  if (!dir.exists(csv_dir)) {
    dir.create(csv_dir)
  }
  if (missing(name)) {
    csv_path <- tempfile(tmpdir = csv_dir, fileext = ".csv")
  } else {
    csv_path <- file.path(csv_dir, paste0(name, ".csv"))
  }
  if (requireNamespace("readr", quietly = TRUE)) {
    readr::write_excel_csv(df, csv_path, na = na)
  } else {
    write.csv(df, csv_path, na = na)
  }
  shell.exec(csv_path)
}

I'm not familiar with jmv, but per the docs, descriptives returns a "results" object that can be converted to a data.frame:

out <- descriptives(data, 
         vars = vars(Mean1, Mean2, Mean3, Mean4),
         sd = TRUE,
         hist = TRUE)

in_excel(as.data.frame(out$descriptives))
  •  Tags:  
  • r
  • Related