本文介绍了合并行,并用group_by和填充空白位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R编码的新手,我一直在尝试使用dplyr的group_by格式化/合并df中的行.但是,我还没到那里.

very new to R coding and I've been trying to format/merge rows in a df using group_by from dplyr. However, I'm not quite there yet.

这是我的数据表的简化.前三个条目共享相同的ID,后两个条目共享相同的ID.

This is a simplification of my data table. The first three entries share the same id, and the last two entries share the same id.

ID                   Assay1  Assay2  Assay3  Assay4  Assay5
13,12 Months,<=-65C  12      NA      NA      NA      NA
13,12 Months,<=-65C  NA      11      NA      NA      NA
13,12 Months,<=-65C  NA      NA      33      NA      NA
09,06 Months,<=-65C  112     NA      NA      NA      NA
09,06 Months,<=-65C  NA      115     NA      NA      NA

我想实现以下目标:

ID                   Assay1  Assay2  Assay3  Assay4  Assay5
13,12 Months,<=-65C  12      11      33      NA      NA
09,06 Months,<=-65C  112     115     NA      NA      NA

以便将AssayValueX中的值合并到每个唯一ID的一行中.我已经将group_by与summary一起使用,但是我不希望有Summary ...我想要一个包含合并行的新df!如果其他功能更适合实现此目的,请告诉我.

So that the values in AssayValueX are merged into one row per unique id. I've used group_by with summarise but I dont want a summary... I want a new df with the merged rows! If other functions are more appropriate to achieve this, please let me know.

推荐答案

您可以使用dplyrzoo

df %>%
    group_by(ID) %>%
    mutate_each(funs(na.locf(., na.rm = FALSE, fromLast = FALSE)))%>%filter(row_number()==n())


ID                   Assay1  Assay2  Assay3  Assay4  Assay5
13,12 Months,<=-65C  12      11      33      NA      NA
09,06 Months,<=-65C  112     115     NA      NA      NA

编辑

关于您的其他问题

数据:

ID Assay1 Assay2 Assay3 Assay4 Assay5
1 13,12 Months,<=-65C     12     13     NA     NA     NA
2 13,12 Months,<=-65C     11     11    999     NA     NA
3 13,12 Months,<=-65C     NA     NA     33     NA     NA
4 09,06 Months,<=-65C    112     NA     NA     NA     NA
5 09,06 Months,<=-65C     NA    115     NA     NA     NA

解决方案

df=df %>%
    group_by(ID) %>%
    summarise_all(funs(toString(na.omit(.))))
df[df=='']=NA
    > df
                   ID Assay1 Assay2  Assay3 Assay4 Assay5
                <chr>  <chr>  <chr>   <chr>  <chr>  <chr>
1 09,06 Months,<=-65C    112    115    <NA>   <NA>   <NA>
2 13,12 Months,<=-65C 12, 11 13, 11 999, 33   <NA>   <NA>

这篇关于合并行,并用group_by和填充空白位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:24