This problem carries three lessons.
- Troubleshooting an error message can be done from the inside out to find the root cause dependably.
- The more involved a function the more likely it is to produce errors. Simpler is generally better.
- Posing a problem can be more difficult than solving it. In keeping with the functional nature of
R
programing, thinking of it in terms of what, rather than how can be helpful. What is wanted is a a return value from expss::cro_cpct for two variables. Doing so would have led to ??cro_cptc where the first positional argument is a
vector/data.frame/list. Variables on which percentage/cases will be computed. Use mrset/mdset for multiple-response variables.
with the additional information that mrset
can be used for more than one variable. That, in turn, leads to ??mrset
where the \dots argument is variables
, which is simply a vector of variable names.
Add a new line at the top
require(expss)
The error from the last line is
Error in .subset2(x, i, exact = exact) : subscript out of bounds
which means that at least banner
is deeply nested and more [[
extractors are needed.
> list(banner)
[[1]]
[[1]][[1]]
VALUES:
NA, NA, 1, 1, NA, 1, NA, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, 1
VALUE LABELS:
1 Availability
[[1]][[2]]
VALUES:
1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, 1
VALUE LABELS:
1 sourcing
It appears, however, that
expss::cro_cpct(expss::mrset(rlang::parse_expr(df[[collist]])
is the culprit.
Changing that to
expss::cro_cpct(expss::mrset(rlang::parse_expr(df[collist])
substitutes a new error
Error: `x` must be a character vector or an R connection
from rlang::parse_expr
, and that function is receiving the df[collist]]
data frame as its argument, which is neither a character vector nor an R connection containing text as a character vector. What would work as an argument is
rlang::parse_expr("Text"))
which simply returns Text
. That renders parse_expr
superfluous and
expss::cro_cpct(expss::mrset("Text"))
would render, as would
tabl <- expss::cro_cpct(expss::mrset(df[collist]))
and adding banner
also works
tabl <- expss::cro_cpct(expss::mrset(df[collist]), banner)
require(expss)
#> Loading required package: expss
data <- mtcars
collist <- c("drat","wt")
data$available <- ifelse(data$vs == 1,1,NA)
data$source <- ifelse(data$am == 1,1,NA)
expss::val_lab(data$available)<-c("Availability"=1)
expss::val_lab(data$source)<-c("sourcing"=1)
banner<-list(data$available,data$source)
df <- data[collist] %>% as.data.frame()
expss::var_lab(colnames(data)[ncol(data)]) <- ""
tabl <- expss::cro_cpct(expss::mrset(df[collist]), banner)
tabl
|
Availability
|
sourcing
|
1.513
|
7.1
|
7.7
|
1.615
|
7.1
|
7.7
|
1.835
|
7.1
|
7.7
|
1.935
|
7.1
|
7.7
|
2.14
|
|
7.7
|
2.2
|
7.1
|
7.7
|
2.32
|
7.1
|
7.7
|
2.62
|
|
7.7
|
2.77
|
|
7.7
|
2.465
|
7.1
|
|
2.76
|
7.1
|
|
2.78
|
7.1
|
7.7
|
2.875
|
|
7.7
|
3.17
|
|
7.7
|
3.54
|
|
7.7
|
3.57
|
|
7.7
|
3.62
|
|
7.7
|
3.08
|
7.1
|
|
3.15
|
7.1
|
|
3.19
|
7.1
|
|
3.215
|
7.1
|
|
3.44
|
14.3
|
|
3.46
|
7.1
|
|
3.69
|
7.1
|
|
3.7
|
7.1
|
|
3.77
|
7.1
|
7.7
|
3.85
|
7.1
|
7.7
|
3.9
|
|
15.4
|
3.92
|
21.4
|
|
4.08
|
14.3
|
15.4
|
4.11
|
7.1
|
7.7
|
4.22
|
7.1
|
15.4
|
4.43
|
|
7.7
|
4.93
|
7.1
|
7.7
|
#Total cases
|
14
|
13
|