tidyr :: pivot_wider中的values_fill参数不起作用

时间:2019-10-31 19:21:56

标签: r

我有此数据:

df_1 <- data.frame(
  x = c(NA, 1, 2, NA, 6), 
  y = c(1, 2, 3, 4, 6)
)

和代码:

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

问题:参数values_fill不起作用。

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))不要用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

出什么问题了?

1 个答案:

答案 0 :(得分:2)

values_fill填充不存在组合的位置。在这里,NA已经是数据集中的值。它保留不变,或者必须在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

即如果我们删除NA行,那么values_fill会在其中丢失组合

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