Skip to content

Commit

Permalink
Combine character and factor show warn. Closes #2317
Browse files Browse the repository at this point in the history
My previous PR did not fix this case.
  • Loading branch information
zeehio committed Feb 27, 2017
1 parent 9d96a10 commit 257ec1b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -347,6 +347,9 @@

* `combine()` accepts `NA` values (#2203, @zeehio)

* `combine()` and `bind_rows()` with character and factor types now always warn
about the coercion to character (#2317, @zeehio)

# dplyr 0.5.0

## Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion inst/include/dplyr/Collecter.h
Expand Up @@ -228,6 +228,7 @@ namespace dplyr {

void collect_factor(const SlicingIndex& index, IntegerVector source) {
CharacterVector levels = get_levels(source);
Rf_warning("binding character and factor vector, coercing into character vector");
for (int i=0; i<index.size(); i++) {
if (source[i] == NA_INTEGER) {
data[index[i]] = NA_STRING;
Expand Down Expand Up @@ -431,7 +432,6 @@ namespace dplyr {
// we however do not assume that they are in the same order
IntegerVector source(v);
CharacterVector levels = get_levels(source);

SEXP* levels_ptr = Rcpp::internal::r_vector_start<STRSXP>(levels);
int* source_ptr = Rcpp::internal::r_vector_start<INTSXP>(source);
for (int i=0; i<index.size(); i++) {
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/helper-combine.R
Expand Up @@ -82,7 +82,8 @@ give_a_warning <- function(item1, item2,
}

# factor and character give a warning when combined (coercion to character)
if (class1 == "factor" && class2 == "character") {
if ((class1 == "factor" && class2 == "character") ||
(class1 == "character" && class2 == "factor")) {
return(TRUE)
}

Expand Down

0 comments on commit 257ec1b

Please sign in to comment.