I have this data:
df_1 <- data.frame(
x = c(NA, 1, 2, NA, 6),
y = c(1, 2, 3, 4, 6)
)
And the code:
library(tidyverse)
df_2 <- df_1 %>%
pivot_longer(cols = c(x, y), names_to = 'factor', values_to = 'values',
values_drop_na = FALSE)
df_2
# A tibble: 10 x 2
factor values
<chr> <dbl>
1 x NA
2 y 1
3 x 1
4 y 2
5 x 2
6 y 3
7 x NA
8 y 4
9 x 6
10 y 6
Problem: argument values_fill
doesn't work.
df_2 %>%
group_by(factor) %>%
mutate(n = row_number()) %>%
pivot_wider(names_from = factor, values_from = values,
values_fill = list(values = 0)) %>%
select(-1)
values_fill = list(values = 0))
don't replace missing values by 0
:
# A tibble: 5 x 2
x y
<dbl> <dbl>
1 NA 1
2 1 2
3 2 3
4 NA 4
5 6 6
What's problem?
values_fill
fills where the combinations doesn't exist. Here, the NA
is already a value in the dataset. It is left as such or have to replace it before the pivot_wider
df_2 %>%
group_by(factor) %>%
mutate(n = row_number())
# A tibble: 10 x 3
# Groups: factor [2]
# factor values n
# <chr> <dbl> <int>
# 1 x NA 1
# 2 y 1 1
# 3 x 1 2
# 4 y 2 2
# 5 x 2 3
# 6 y 3 3
# 7 x NA 4
# 8 y 4 4
# 9 x 6 5
#10 y 6 5
i.e. if we remove the NA
rows, then there would be missing combinations where the values_fill
act upon
df_2 %>%
group_by(factor) %>%
mutate(n = row_number()) %>%
ungroup %>%
filter(!is.na(values)) %>%
pivot_wider(names_from = factor, values_from = values,
values_fill = list(values = 0))
# A tibble: 5 x 3
# n y x
# <int> <dbl> <dbl>
#1 1 1 0
#2 2 2 1
#3 3 3 2
#4 4 4 0
#5 5 6 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With