Closed joethorley closed 4 years ago
If I define level.term I end up with a factor whether or not I've set stringsAsFactors = FALSE
levels.term <- function(x) unique(as.character(x))
x <- rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)), stringsAsFactors = FALSE)
class(x$term)
#> [1] "factor"
This is not what I see:
rbind(
data.frame(term = term::term(x = 1)),
data.frame(term = term::term(x = 1))
)
#> term
#> 1 x
#> 2 x
Created on 2020-02-06 by the reprex package (v0.3.0)
Could you please post a reprex?
Rerunning your reprex with session info:
rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)))
#> term
#> 1 x
#> 2 x
Created on 2020-02-06 by the reprex package (v0.3.0)
Maybe the dev version of vctrs does the magic?
It works because I had added
#' @export
levels.term <- function(x) unique(as.character(x))
If you go to the broken-rbind-demo
branch you will get
rbind(
data.frame(term = term::term(x = 1)),
data.frame(term = term::term(x = 1))
)
#> Error: `levels.term()` not supported.
Created on 2020-02-06 by the reprex package (v0.3.0)
with
Backtrace:
█
1. └─base::rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)))
2. └─base::rbind(deparse.level, ...)
3. ├─[ base::levels(...) ]
4. └─vctrs:::levels.vctrs_vctr(xj)
5. └─vctrs:::stop_unsupported(x, "levels")
6. └─vctrs:::stop_vctrs(...)
The hack is a pain because rbind turns the term vector into a factor
dput(rbind(
data.frame(term = term::term(x = 1)),
data.frame(term = term::term(x = 1))
))
#> structure(list(term = structure(c(1L, 1L), .Label = "x", class = "factor")), row.names = c(NA,
#> -2L), class = "data.frame")
Created on 2020-02-06 by the reprex package (v0.3.0)
I get the same result with the development branch of vctrs.
Created on 2020-02-04 by the reprex package (v0.3.0)