Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crosstab fails if 1st variable has spaces, a pipe, and multiple quoted arguments in its name #87

Closed
sfirke opened this issue Jan 8, 2017 · 2 comments

Comments

@sfirke
Copy link
Owner

sfirke commented Jan 8, 2017

Wildly obscure, but I found it when in the course of actual analysis I tried:

crosstab(responses_principals$`The candidate(s) applied directly to my school` %>% convert_to_top_2_agree(c("All", "Most")),
            responses_principals$region) 

and got:

Error in stats::setNames(., c(var_name, names(.)[-1])) :
'names' attribute [11] must be the same length as the vector [10]

Here is a reprex:

dat <- data.frame(
  `The candidate(s) applied directly to my school` = c("a", "b", "a", "b"),
  x = 1:4,
  check.names = FALSE,
  stringsAsFactors = FALSE
)

crosstab(dat$`The candidate(s) applied directly to my school` %>% na_if(c("a", "c", "d", "e")), dat$x)
@sfirke
Copy link
Owner Author

sfirke commented Jan 8, 2017

Looks like it's the line var_name <- deparse(substitute(vec1)) in crosstab(), which in this case returns a vector of length 2 to assign to var_name.

Adding this immediately after var_name is assigned seems to fix it, and be safe; I will write a test for it and then merge in:

  # an odd variable name can be deparsed into a vector of length >1, rare but breaks function, see issue #87
  if(length(var_name) > 1){ var_name <- paste(var_name, collapse = "") }

@sfirke sfirke closed this as completed in d3dd992 Jan 8, 2017
@sfirke
Copy link
Owner Author

sfirke commented Jan 24, 2017

It's the fault of the width.cutoff argument in deparse(): http://stackoverflow.com/q/41596098/4470365. I like the fix above as more robust than just increasing width.cutoff to a very high number. But maybe I should also increase it, if it would result in prettier results than the collapsed version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant