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

vctrs can coerce c(NA) logical to character, but not logical(0) to character. Why? #1958

Open
Ljupch0 opened this issue Dec 23, 2024 · 0 comments

Comments

@Ljupch0
Copy link

Ljupch0 commented Dec 23, 2024

vctrs::vec_c("a", NA) # WORKS
vctrs::vec_c("a", logical(0)) # FAILS

This issue has broken my R prod code more than any other. I would like to understand why.

Usually I have bind_rows combine some dataframes. Sometimes the dataframes have all NA columns that are read-in as logical.
When I bind these dataframes with others that have values in these columns, type conversion works and I don't have a problem. But if it happens that all dataframe rows are deleted before merging, like in a shiny reactive df for instance, then bind_rows fails with message Can't combine ..1<character> and..2 <logical>.

Example:

df_with_na <- tibble(
  column = c(NA, NA, NA)
)

# Column is logical
# class(df_with_na$column)))

print(
  bind_rows(
    df_with_na,
    tibble(
      column = c("a", "b", "c")
    )
  )
)

# Binding works, logical made of NAs is combined with character.

# Rows are removed from the df.
df_without_rows <- df_with_na[0,]


print(class(df_without_rows$column))

bind_rows(
  df_without_rows,
  tibble(
    column = c("a", "b", "c")
  )
)

Knowing that it happens, I can be proactive and drop all NA columns, or skip binding dataframes with 0 rows. But I'm curious why this behavior happens.

Is it that an all NA vector is a "fake" logical, while logical(0) could be a real logical vector comprised of booleans?

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

No branches or pull requests

1 participant