R将多行汇总为一,缺少值

时间:2018-07-07 00:25:50

标签: r aggregate

我具有以下格式的数据集

Date  Time  V1 V2 V3 ....

2018-02-18 09:51:47               NA              0.38               NA

2018-02-18 09:51:47               NA               NA                Quiet

2018-02-18 09:51:47               Normal               NA                NA

I want to merge the rows by Date and Time, and return

2018-02-18 09:51:47               Normal              0.38             Quiet

我尝试了聚合函数,但它只返回第一行

aggregate(feb18[-grep("^(Date|Time)$", names(feb18))], feb18[c("Date", "Time")], FUN=function(x)  if(all(is.na(x))) NA else x[!is.na(x)][1])

3 个答案:

答案 0 :(得分:0)

使用dplyr

df %>% 
  group_by(Date, Time) %>%
  summarize(V1=max(V1, na.rm=T),
            V2=sum(V2, na.rm=T),
            V3=max(V3, na.rm=T))
# A tibble: 1 x 5
# Groups:   Date [?]
  Date       Time         V1        V2 V3   
  <date>     <S4: Period> <chr>  <dbl> <chr>
1 2018-02-18 9H 51M 47S   Normal 0.380 Quiet

答案 1 :(得分:0)

aggregate(.~Date+Time,df,function(x)x[!is.na(x)],na.action = identity)
        Date     Time     V1   V2    V3
1 2018-02-18 09:51:47 Normal 0.38 Quiet


  library(tidyverse)
   df%>%group_by(Date,Time)%>%
      summarise_all(~coalesce(!!!as.list(.)))
# A tibble: 1 x 5
# Groups:   Date [?]
  Date       Time     V1        V2 V3   
  <chr>      <chr>    <chr>  <dbl> <chr>
1 2018-02-18 09:51:47 Normal 0.380 Quiet

df%>%group_by(Date,Time)%>%
   summarise_all(~.x[!is.na(.x)])
# A tibble: 1 x 5
# Groups:   Date [?]
  Date       Time     V1        V2 V3   
  <chr>      <chr>    <chr>  <dbl> <chr>
1 2018-02-18 09:51:47 Normal 0.380 Quiet

答案 2 :(得分:0)

使用data.table

library(data.table)
setDT(df)[, lapply(.SD, na.omit), by = .(Date, Time)]
#         Date     Time     V1   V2    V3
#1: 2018-02-18 09:51:47 Normal 0.38 Quiet