Group (collapse?) rows without summarising to fill in NAs

和自甴很熟 提交于 2021-01-29 08:07:52

问题


I have an issue that should be straightforward with dplyr (I think) but I can't seem to find a resolution.

My dataframe comprises numbers and factors. Each observation is represented by two rows which have either a value or NA in one of two columns (Agg_Entropy and Av_Amplitude). I want to combine each observation's rows into a single row (without summarising), so that the NAs are replaced with the relevant values.

A simple excerpt of the dataframe:

 Selection Low   High    Agg_Entropy Av_Amplitude Filename                  
  <fct>     <fct> <fct>         <dbl>        <dbl> <fct>                     
1 1         368.2 13747.8       NA           -17.5 20180110_182800_Sunset.wav
2 1         368.2 13747.8        5.62         NA   20180110_182800_Sunset.wav
3 2         142   13926.3       NA           -17.4 20180110_182800_Sunset.wav
4 2         142   13926.3        5.96         NA   20180110_182800_Sunset.wav

What I want:

 Selection   Low    High Agg_Entropy Av_Amplitude                   Filename
1         1 368.2 13747.8       5.623        -17.5 20180110_182800_Sunset.wav
2         2 142.0 13926.3       5.958        -17.4 20180110_182800_Sunset.wav

Any help is very much appreciated. Thank you!


回答1:


After group_by with columns 'Selection', 'Filename', 'Low', and 'High', summarise the other columns by removing the NA elements with na.omit. Here, we assume that there are only one non-NA element per each column for the groups

library(tidyverse)
df1 %>%
   group_by(Selection, Filename, Low, High) %>%
   summarise_all(na.omit)


来源:https://stackoverflow.com/questions/53975397/group-collapse-rows-without-summarising-to-fill-in-nas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!