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
The object output from describeBy
is a list
. We could rbind
them to a single matrix or data.frame and it should work
do.call(rbind, describeBy(mtcars[, 'mpg'], mtcars[, 'vs']) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.