I have a large data frame that I summarize in multiple ways using describeBy
from the psych
library as follows:
library(tidyverse)
library(openxlsx)
library(psych)
.
.
.
# Describe by Region
lst1 <- describeBy(df[QUESTIONS], df[REGION_DESCRIPTOR])
# Describe by Doctor
lst2 <- describeBy(df[QUESTIONS], df[CARE_DESCRIPTOR])
I then create a new workbook
wb = createWorkbook()
and start trying to write lst1, lst2,.... into into it, one item per worksheet:
addWorksheet(wb, REGION_DESCRIPTOR)
writeData(wb, REGION_DESCRIPTOR, lst1)
addWorksheet(wb, CARE_DESCRIPTOR)
writeData(wb, CARE_DESCRIPTOR, lst2)
Unfortunately, I get an error message:
Error in as.data.frame.default(x, stringsAsFactors = FALSE) :
cannot coerce class ‘c("psych", "describeBy")’ to a data.frame
How can I write each describeBy
object to a worksheet using openxlsx
? I have tried using writexl
, and while it works, I am not happy with the fact that it writes each of the summmaries generated by describeBy
to a different worksheet. As I have close to a dozen describeBy
's, each with 3-5 categories, this rapidly becomes unwieldy.
Thank you in advance for your help
Thomas Philips