【问题标题】:pivot_wider does not seem to work with missing values. How to turn spread() into pivot_wider() when there is missing valuespivot_wider 似乎不适用于缺失值。缺少值时如何将 spread() 转换为 pivot_wider()
【发布时间】:2019-12-13 17:14:02
【问题描述】:

由于 spread() 函数被新的 pivot_wider() 函数取代,我从现在开始尝试使用 pivot_wider() 但由于缺少值,它似乎不起作用。非常感谢任何帮助

# This is an example I saw on the web

surveys <- read.csv("http://kbroman.org/datacarp/portal_data_joined.csv",
                    stringsAsFactors = FALSE)
library(dplyr)

surveys %>%
  filter(taxa == "Rodent",
         !is.na(weight)) %>%
  group_by(sex,genus) %>%
  summarize(mean_weight = mean(weight)) %>% 
  spread(sex, mean_weight)```

#It gives me the following output. This is what I would like to get
# A tibble: 10 x 4
   genus              V1      F      M
   <chr>           <dbl>  <dbl>  <dbl>
 1 Baiomys          NA     9.16   7.36
 2 Chaetodipus      19.8  23.8   24.7 
 3 Dipodomys        81.4  55.2   56.2 
 4 Neotoma         168.  154.   166.  
 5 Onychomys        23.4  26.8   26.2 
 6 Perognathus       6     8.57   8.20
 7 Peromyscus       19.9  22.5   20.6 
 8 Reithrodontomys  11.1  11.2   10.2 
 9 Sigmodon         70.3  71.7   61.3 
10 Spermophilus     NA    57    130  
surveys %>%
  filter(taxa == "Rodent",
         !is.na(weight)) %>%
  group_by(sex,genus) %>%
  summarize(mean_weight = mean(weight)) %>%
  pivot_wider(
    names_from = sex,
    values_from = mean_weight,
    names_repair = "minimal"
    )

It says the following
Error: Column 1 must be named.
Use .name_repair to specify repair.
Run `rlang::last_error()` to see where the error occurred.

【问题讨论】:

    标签: r tidyr


    【解决方案1】:

    在旋转之前替换性别中的缺失值:

    surveys %>%
      filter(taxa == "Rodent",
             !is.na(weight)) %>%
      group_by(sex,genus) %>%
      summarize(mean_weight = mean(weight)) %>%
      ungroup() %>% 
      mutate(sex = if_else(sex == "", "unknown", sex)) %>% 
      pivot_wider(
        names_from = sex,
        values_from = mean_weight,
        names_repair = "minimal"
      )
    

    【讨论】:

    • 谢谢乔瓦尼。它运作良好。我很感激
    【解决方案2】:

    如果您不想对数据进行预处理,pivot_wider 最近获得了一个新参数来协助缺失因子水平 (https://cran.r-project.org/web/packages/tidyr/news/news.html):

    “pivot_wider() 获得了新的 names_expand 和 id_expand 参数,用于将隐式缺失因子级别和变量组合转换为显式。这类似于 spread() 中的 drop 参数”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2020-05-21
      • 1970-01-01
      • 2019-12-31
      • 2014-10-09
      • 2020-06-17
      • 1970-01-01
      相关资源
      最近更新 更多