ivot_wider()不会将变量折叠为单行

时间:2020-03-26 19:36:15

标签: r pivot

我的数据集是这样的

Organism    Reads    Name
Name1       10       MF60
Name2       100      MF60
Name1       50       MF60
Name2       75       AFM10
Name3       200      AFM10

当我使用ivot_wider()时,

df_wide <- df %>%
    pivot_wider(names_from = Organism, values_from = Reads)

我得到的输出看起来像这样

Name   Name1  Name2  Name3
MF60    10     NA     NA
MF60    NA     100    NA
MF60    NA     NA     50
AMF10   75     NA     NA
AMF10   NA     200    NA
etc..

为什么不将所有MF60折叠成一行?我试过更改names_from和values_from值,但是它不起作用。谢谢您的见识!

1 个答案:

答案 0 :(得分:1)

问题与实际数据集中的额外“百分比”列有关,而该列不在上面的样本集中。由于您的数据集有一个未在ivot_wider函数中指定的额外列,因此每一行都将被唯一对待,因此将导致不正确的方阵。

如果排除了多余的列(百分比),并且仅选择所需的列(有机体,读物和名称),pivot_wider的行为将符合预期。

df-structure(list(Organism = c("Unclassified", "Peptoniphilus methioninivorax", "Myroides odoratimimus", "Enterococcus sulfureus", "Peptoniphilus olsenii", "Dysgonomonas wimpennyi"), 
      Reads = c(95345, 78055, 63769, 14849, 14229, 13245), 
      Percentage = c(28.95375, 23.70324, 19.36496, 4.509248, 4.320971, 4.022156),  
      Name = c("Marg F6O", "Marg F6O", "Marg F6O", "Marg F6O", "Marg F6O", "Marg F6O")), 
      row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

library(tidyr)
df %>%  pivot_wider(names_from = Organism, values_from = Reads)
## Does not provide the desired Results!

工作版本:

library(dplyr)
df %>% select(-Percentage) %>%  pivot_wider(names_from = Organism, values_from = Reads)

# # A tibble: 1 x 7
# Name   Unclassified `Peptoniphilus met~ `Myroides odorat~ `Enterococcus su~ `Peptoniphilus o~ `Dysgonomonas wi~
#   <chr>         <dbl>               <dbl>             <dbl>             <dbl>             <dbl>             <dbl>
#   1 Marg ~        95345               78055             63769             14849             14229