Skip to content

Commit

Permalink
factor levels
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Jan 3, 2024
1 parent 4a1c7b0 commit af296aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: geometries
Type: Package
Title: Convert Between R Objects and Geometric Structures
Date: 2023-06-22
Version: 0.2.3
Date: 2024-01-03
Version: 0.2.4
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# 0.2.4

* factor levels remain [sfheaders issue 89](https://github.com/dcooley/sfheaders/issues/89)

# 0.2.2

* removed C++ system requirements
Expand Down
15 changes: 15 additions & 0 deletions inst/include/geometries/utils/unique/unique_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define R_GEOMETRIES_UTILS_UNIQUE_SORT_H

#include <Rcpp.h>
#include "geometries/utils/attributes/attributes.hpp"

namespace geometries {
namespace utils {
Expand Down Expand Up @@ -35,6 +36,20 @@ namespace utils {
return sexp_unique< double, REALSXP >( s2 );
}
case INTSXP: {
// issue 89 (sfheaders - https://github.com/dcooley/sfheaders/issues/89)
if( Rf_isFactor(s2) ) {
Rcpp::IntegerVector iv = Rcpp::as< Rcpp::IntegerVector >( s2 );
Rcpp::List attributes = Rcpp::List::create(
Rcpp::_["levels"] = iv.attr("levels"),
Rcpp::_["class"] = iv.attr("class")
);

SEXP res = sexp_unique< int, INTSXP >( s2 );
geometries::utils::attach_attributes( res, attributes );
return res;
}


return sexp_unique< int, INTSXP >( s2 );
}
case STRSXP: {
Expand Down
6 changes: 6 additions & 0 deletions inst/tinytest/test_unique.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ expect_equal( geometries:::.test_unique_sort( c(1L,3L,3L,2L) ), c(1L,3L,2L) )
expect_equal( geometries:::.test_unique_sort( c(1,3,3,2) ), c(1,3,2) )
expect_equal( geometries:::.test_unique_sort( c(T,F,F,T) ), c(T,F) )

## Sorting factor - levels remain
f <- as.factor(c(1,2,3))
expect_equal(geometries:::.test_unique_sort(f), f)

df <- data.frame(fact = f)
expect_equal(geometries:::.test_unique_ids(df, 0L), f)

0 comments on commit af296aa

Please sign in to comment.