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

incorrect output when using pivot_longer with values_drop_na = TRUE #484

Open
wjchulme opened this issue Dec 20, 2024 · 0 comments
Open
Labels
bug an unexpected problem or unintended behavior

Comments

@wjchulme
Copy link

There appears to be an issue with the values_drop_na = TRUE option when using pivot_longer with dtplyr. The names_to columns end up as odd character-rendered list-cols things, rather than simple vectors. See example:

library('dtplyr')
library('dplyr')
library('tidyr')

example.data <- data.frame(
  pid = c(1,2,3,1,2,3),
  valueA_1 = runif(6),
  valueA_2 = NA_real_,
  valueB_1 = runif(6),
  valueA_2 = NA_real_,
  type_1 = c("A","B", "C", "C", "B", "B"),
  type_2 = NA_character_
)

example.data %>%
  lazy_dt() %>%
  pivot_longer(
    cols = -pid,
    names_to = c(".value", "index"),
    names_pattern = "^(.*)_(\\d+)",
    values_drop_na = TRUE,
  ) %>% 
  as_tibble()

Which produces:

# A tibble: 6 × 5
    pid index                                                                                   valueA valueB type 
  <dbl> <chr>                                                                                    <dbl>  <dbl> <chr>
1     1 "c(\"1\", \"1\", \"1\", \"1\", \"1\", \"1\", \"2\", \"2\", \"2\", \"2\", \"2\", \"2\")"  0.266 0.945  A    
2     2 "c(\"1\", \"1\", \"1\", \"1\", \"1\", \"1\", \"2\", \"2\", \"2\", \"2\", \"2\", \"2\")"  0.372 0.661  B    
3     3 "c(\"1\", \"1\", \"1\", \"1\", \"1\", \"1\", \"2\", \"2\", \"2\", \"2\", \"2\", \"2\")"  0.573 0.629  C    
4     1 "c(\"1\", \"1\", \"1\", \"1\", \"1\", \"1\", \"2\", \"2\", \"2\", \"2\", \"2\", \"2\")"  0.908 0.0618 C    
5     2 "c(\"1\", \"1\", \"1\", \"1\", \"1\", \"1\", \"2\", \"2\", \"2\", \"2\", \"2\", \"2\")"  0.202 0.206  B    
6     3 "c(\"1\", \"1\", \"1\", \"1\", \"1\", \"1\", \"2\", \"2\", \"2\", \"2\", \"2\", \"2\")"  0.898 0.177  B    
Warning message:
In `[.data.table`(melt(`_DT27`, measure.vars = list(c("valueA_1",  :
  Coercing 'list' RHS to 'character' to match the type of column 2 named 'index'.

The correct output (which can be seen by removing the lazy_dt() line in the above code) should be:

# A tibble: 6 × 5
    pid index valueA valueB type 
  <dbl> <chr>  <dbl>  <dbl> <chr>
1     1 1      0.266 0.945  A    
2     2 1      0.372 0.661  B    
3     3 1      0.573 0.629  C    
4     1 1      0.908 0.0618 C    
5     2 1      0.202 0.206  B    
6     3 1      0.898 0.177  B   
@markfairbanks markfairbanks added the bug an unexpected problem or unintended behavior label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants