1

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?

zucchini
  • 33
  • 4
  • 5
    What package is this? It's not obvious from your question, and the word "descriptives" is a bit more difficult to search for R packages. – r2evans Feb 17 '22 at 19:58
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Feb 17 '22 at 20:09
  • 1
    You might wanna check `datapasta`: https://github.com/MilesMcBain/datapasta – Ruam Pimentel Feb 17 '22 at 20:27
  • The function is from the jmv package – zucchini Feb 17 '22 at 20:39

2 Answers2

1

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)


Bloxx
  • 1,495
  • 1
  • 9
  • 21
0

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))
zephryl
  • 14,633
  • 3
  • 11
  • 30