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

quantities does not work with unique() in tibbles #12

Closed
vorpalvorpal opened this issue Jun 24, 2021 · 2 comments
Closed

quantities does not work with unique() in tibbles #12

vorpalvorpal opened this issue Jun 24, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@vorpalvorpal
Copy link

df <- tribble(
  ~a, ~b,
  "foo", set_quantities(1, mm, 1),
  "foo", set_quantities(1, mm, 1)
)
unique(df)
A tibble: 2 x 2
  a              b
  <chr> ± err [mm]
1 foo        1 ± 1
2 foo        1 ± 1

I had a more complex tibble with quantities where calling unique() wasn't working so I tested "df[1,] == df[2,]" and got the following error:

Warning message:
In '==' : boolean operators not defined for 'errors' objects, uncertainty dropped

However, I can't seem to get this error again with the minimal working example above even though unique() isn't working correctly.

@Enchufa2
Copy link
Member

Thanks, unique was only recently added to units, we'll need to implement this method for quantities and errors too.

@Enchufa2 Enchufa2 added the enhancement New feature or request label Jun 24, 2021
@Enchufa2
Copy link
Member

Enchufa2 commented Jan 5, 2022

Unfortunately, calling unique on the whole data frame still won't work. This is due to how unique.data.frame works, and we can't do anything about it. However, since we have now a method for duplicated, you can do this:

library(quantities)
#> Loading required package: units
#> udunits database from /usr/share/udunits/udunits2.xml
#> Loading required package: errors

df <- tibble::tribble(
  ~a, ~b,
  "foo", set_quantities(1, mm, 1),
  "foo", set_quantities(1, mm, 1)
)

unique_quantities <- function(df, ...) {
  dup <- rowSums(sapply(df, duplicated, ...)) == ncol(df)
  df[dup, ]
}

unique_quantities(df)
#> # A tibble: 1 × 2
#>   a              b
#>   <chr> (err) [mm]
#> 1 foo         1(1)

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

No branches or pull requests

2 participants