Error when using `tidyverse` to reshape data with `pivot_longer()` and handling NAs
Could someone explain Hey everyone, I'm running into an issue that's driving me crazy... I'm trying to reshape my data using the `pivot_longer()` function from the `tidyverse` package in R, but I'm running into some unexpected behavior with NA values in my dataset. The initial data frame looks like this: ```r library(tidyverse) data <- tibble( id = 1:5, type_a = c(1, NA, 3, 4, NA), type_b = c(NA, 2, NA, 5, 6) ) ``` I want to convert this wide format into a long format, where each type (type_a, type_b) becomes a row in a new column called `type`. However, when I try the following command: ```r long_data <- data %>% pivot_longer(cols = starts_with("type"), names_to = "type", values_to = "value") ``` I get the following output: ```r # A tibble: 10 Ã 3 id type value <int> <chr> <dbl> 1 1 type_a 1 2 1 type_b NA 3 2 type_a NA 4 2 type_b 2 5 3 type_a 3 6 3 type_b NA 7 4 type_a 4 8 4 type_b 5 9 5 type_a NA 10 5 type_b 6 ``` I expected that the resulting data frame would remove the rows where `value` is NA, but it still includes them. I've tried adding `na.omit()` after the `pivot_longer()` call: ```r long_data <- data %>% pivot_longer(cols = starts_with("type"), names_to = "type", values_to = "value") %>% na.omit() ``` While this works, it feels inefficient because I'm removing NAs after the transformation. I would prefer to filter them out during the pivoting process if possible. Is there a way to exclude NAs while using `pivot_longer()`? Or is there a more optimal approach to handle NAs in this situation? I'm currently using `tidyverse` version 1.3.1. This is part of a larger service I'm building. Any help would be greatly appreciated! I'm using R latest in this project. Any pointers in the right direction?