ekstroem / dataReporter

85 stars 4 forks source link

Error when variable has NA as factor level #1

Closed JeffreyRStevens closed 3 years ago

JeffreyRStevens commented 3 years ago

First, thank you for this awesome package!

I'm receiving an error whenever I try to run dataReporter on a data frame where a variable has NA as a factor level. The report is generated, but it does not analyze the variable with NA as a factor level or any variable after that. This error seems to come from the identifyNums() function.

Here is an example:

library(dataReporter)
# Every thing is fine when NAs are not included as factor levels
mycolumn <- as.factor(c("Yes", "No", "Yes", "No", NA)) 
mydf <- data.frame(mycolumn)
levels(mydf$mycolumn)
#> [1] "No"  "Yes"
makeCodebook(mydf, replace = TRUE)
#> Data report generation is finished. Please wait while your output file is being rendered.
#
# But when there NA is a factor level...
mycolumn2 <- droplevels(mycolumn, exclude = NULL)
mydf2 <- data.frame(mycolumn2)
levels(mydf2$mycolumn2)
#> [1] "No"  "Yes" NA
makeCodebook(mydf2, replace = TRUE)
#> Error in if (sum(nchar(v)) == 0) { : 
#>   missing value where TRUE/FALSE needed
#> Data report generation is finished. Please wait while your output file is being rendered.

Created on 2021-03-10 by the reprex package (v1.0.0)

annennenne commented 3 years ago

Thanks for reporting the problem! I think I have fixed it now so that the code runs. You will need to download and use the development version of the package to get the new version:

library(devtools)
install_github("estroem/dataReporter")

I must warn you, though, that dataReporter generally handles NAs by omitting them. I'm not sure how that interplays with NA being used as a factor level, so you might find other weird behavior.