Error in `as.character.factor(X, ...)`: malformed factor

testthat::test_that('All options in the descriptives work for cont vars without split by (sunny)', {
tCriticalValue <- 1 - ((1 - CI_WIDTH) / 2)
ciLower <- mean - qt(tCriticalValue, df=n-1) * se
ciUpper <- mean + qt(tCriticalValue, df=n-1) * se
testthat::expect_equal(n, r[["x[n]"]], tolerance = 1e-5)
testthat::expect_equal(missing, r[["x[missing]"]], tolerance = 1e-5)
testthat::expect_equal(mean, r[["x[mean]"]], tolerance = 1e-5)
testthat::expect_equal(se, r[["x[se]"]], tolerance = 1e-5)
testthat::expect_equal(ciLower, r[["x[ciLower]"]], tolerance = 1e-5)
testthat::expect_equal(ciUpper, r[["x[ciUpper]"]], tolerance = 1e-5)
testthat::expect_equal(median(x), r[["x[median]"]], tolerance = 1e-5)
testthat::expect_equal(mode, r[["x[mode]"]], tolerance = 1e-5)
testthat::expect_equal(sum(x), r[["x[sum]"]], tolerance = 1e-5)
testthat::expect_equal(sd(x), r[["x[sd]"]], tolerance = 1e-5)
testthat::expect_equal(var(x), r[["x[variance]"]], tolerance = 1e-5)
testthat::expect_equal(IQR(x), r[["x[iqr]"]], tolerance = 1e-5)
testthat::expect_equal(range(x)[2] - range(x)[1], r[["x[range]"]], tolerance = 1e-5)
testthat::expect_equal(min(x), r[["x[min]"]], tolerance = 1e-5)
testthat::expect_equal(max(x), r[["x[max]"]], tolerance = 1e-5)
testthat::expect_equal(0.11014, r[["x[skew]"]], tolerance = 1e-5)
testthat::expect_equal(0.24138, r[["x[seSkew]"]], tolerance = 1e-5)
testthat::expect_equal(-0.11958, r[["x[kurt]"]], tolerance = 1e-5)
testthat::expect_equal(0.47833, r[["x[seKurt]"]], tolerance = 1e-5)
testthat::expect_equal(as.numeric(shapiro$statistic), r[["x[sww]"]], tolerance = 1e-5)
testthat::expect_equal(as.numeric(shapiro$p.value), r[["x[sw]"]], tolerance = 1e-5)
testthat::expect_equal(as.numeric(quantiles[1]), r[["x[quant1]"]], tolerance = 1e-5)
testthat::expect_equal(as.numeric(quantiles[2]), r[["x[quant2]"]], tolerance = 1e-5)
testthat::expect_equal(as.numeric(quantiles[3]), r[["x[quant3]"]], tolerance = 1e-5)
testthat::test_that('Descriptives transposed table works with splitBy', {
group=sample(letters[1:3], 100, replace = TRUE)
data=df, vars=vars(Q1, Q2, Q3, Q4), splitBy=group, desc="rows"
testthat::test_that("Frequency table is displayed correctly for empty data set", {
desc <- jmv::descriptives(data = df, vars = "dep", splitBy = "group", freq = TRUE)
testthat::test_that("Non-grouped frequency table is displayed correctly", {
dep = factor(sample(letters[1:3], 100, replace = TRUE), levels = letters[1:3])
desc <- jmv::descriptives(data = df, vars = "dep", freq = TRUE)
testthat::test_that("Grouped frequency table is displayed correctly", {
dep = factor(sample(1:3, 100, replace = TRUE), levels = 1:3),
group = factor(sample(letters[1:3], 100, replace = TRUE), levels = letters[1:3])
testthat::test_that("Weighted grouped frequency table is displayed correctly", {
df <- data.frame(var = rep(LETTERS[1:5]), stringsAsFactors = TRUE)
# AND weights associated with these values
# WHEN the frequency tables are calculated
desc <- jmv::descriptives(data = df, vars = c("var"), freq = TRUE)
# THEN the counts in the frequency table are correct
testthat::expect_equal(r$counts, param$expected_counts, info = param$info)
testthat::test_that('Descriptives works old scenario', {
desc <- jmv::descriptives(data, vars=c("w", "y", "z"), splitBy = "x",
testthat::expect_equal(c(2, 1, 1, 1, 2, 1, 1, 1, 2), freq$counts)
# Test descriptives table numerical values
testthat::expect_equal(2.619, descr$`y[seKurtb]`, tolerance = 1e-3)
testthat::expect_equal(-1.289, descr$`z[kurtc]`, tolerance = 1e-3)
testthat::expect_equal(1, descr$`z[missinga]`, tolerance = 1e-3)
testthat::expect_equal(5.750, descr$`y[meana]`, tolerance = 1e-3)
testthat::expect_equal(-2, descr$`z[modeb]`, tolerance = 1e-3)
testthat::expect_equal(4, descr$`y[mina]`, tolerance = 1e-3)
testthat::expect_equal(2.25, descr$`y[perc1c]`, tolerance = 1e-3)
testthat::test_that('Histogram is created for nominal numeric variable', {
a2 = factor(sample(1:10, 100, replace = TRUE))
desc <- jmv::descriptives(data, c('a1', 'a2'), hist=TRUE)
testthat::test_that("No error is thrown when an empty factor is used as variable", {
data <- data.frame(x = factor(rep(NA, 10)))
result <- jmv::descriptives(data, "x", freq = TRUE)
testthat::test_that('Sensible error message is provided when splitBy var contains no data', {
"The 'split by' variable 'group' contains no data."
testthat::test_that('Extreme values table works', {
ordinal = sample(1:7, 100, replace = TRUE),
character = factor(sample(letters[1:7], 100, replace = TRUE))
data=df, vars=c("numeric", "ordinal", "character"), extreme=TRUE, extremeN=extremeN
testthat::test_that('Extreme values provides note if number of cases is lower than extremeN', {
r <- jmv::descriptives(data=df, vars="x", extreme=TRUE, extremeN=extremeN)
"Number of requested extreme values is higher than the number of rows in the data."
testthat::expect_equal(eDf$value, c(3.1, 2.3, 1.1, NA, NA, 1.1, 2.3, 3.1, NA, NA))
testthat::test_that("Weighted descriptives with no grouping variable works", {
# GIVEN a data frame with a numeric variable
data = df, vars = "x", desc="rows", sum = TRUE, variance = TRUE, range = TRUE
# THEN the statistics are calculated correctly
testthat::expect_equal(r$n, param$expected$n, info = param$info)
testthat::expect_equal(r$mean, param$expected$mean, tolerance = 1e-3, info = param$info)
testthat::expect_equal(r$median, param$expected$median, info = param$info)
testthat::expect_equal(r$sum, param$expected$sum, info = param$info)
testthat::expect_equal(r$sd, param$expected$sd, tolerance = 1e-3, info = param$info)
testthat::expect_equal(r$variance, param$expected$variance, tolerance = 1e-3, info = param$info)
testthat::expect_equal(r$min, param$expected$min, info = param$info)
testthat::expect_equal(r$max, param$expected$max, info = param$info)
testthat::expect_equal(r$range, param$expected$range, info = param$info)
testthat::test_that("Weighted descriptives with grouping variable works", {
# GIVEN a data frame with a numeric variable and a grouping variable
df <- data.frame(group = rep(LETTERS[1:2], length.out=10), x = rep(1:5, each=2))
testthat::expect_equal(r$n, c(5, 10), info = param$info)
testthat::expect_equal(r$mean, c(3, 3), info = param$info)
testthat::expect_equal(r$median, c(3, 3), info = param$info)
testthat::expect_equal(r$sum, c(15, 30), info = param$info)
testthat::expect_equal(r$sd, c(1.581, 1.491), tolerance = 1e-3, info = param$info)
testthat::expect_equal(r$variance, c(2.500, 2.222), tolerance = 1e-3, info = param$info)
testthat::expect_equal(r$min, c(1, 1), info = param$info)
testthat::expect_equal(r$max, c(5, 5), info = param$info)
testthat::expect_equal(r$range, c(4, 4), info = param$info)
testthat::test_that("Splitting by multiple variables works for normal table", {
# GIVEN a data frame with a numeric variable and two grouping variables
# WHEN the descriptives split by multiple groups are calculated
# THEN the descriptives are calculated correctly
testthat::test_that("Splitting by multiple variables works for transposed table", {
# WHEN the transosed descriptives split by multiple groups are calculated