【问题标题】:R collapsing multiple rows into one row by grouping multiple columnsR通过分组多列将多行折叠成一行
【发布时间】:2019-10-08 18:34:42
【问题描述】:

我想通过对多列而不是其他列进行分组来将多行折叠成一行。我在不用于分组的列中有 NA。在尝试了多种解决方案后,结果表中充满了 NA,没有值。我能够使解决方案起作用,但前提是我使 is.na = 0。我不想将 0 引入数据框中,因为某些测量结果为零。

这是一个后续 R collapse multiple rows into 1 row - same columns 我尝试了所有推荐的解决方案,数据结果为 NA

TreatName<-c('Static','Static','Dynamic', 'Static')
id<-c('patient1','patient1','patient2','patient2')
Method<-c('IV', 'IV', 'IV', 'IV')
drug1<-as.numeric(c(34,'','',''))
drug2<-as.numeric(c('',7,'',''))
drug3<-as.numeric(c('','',56, 0))
df<-data.frame(TreatName, id, Method, drug1, drug2, drug3)

library(plyr)
groupColumns = c("TreatName","id", "Method")
dataColumns = c( "drug1", "drug2","drug3")
df1<-ddply(df, groupColumns, function(x) colSums(x[dataColumns]))


The expected result should be 
TreatName   id  Method  drug1   drug2   drug3
Static  patient1    IV  34  7   NA
Dynamic patient2    IV  NA  NA  56
Static  patient2    IV  NA  NA  0

The actual results are
TreatName   id  Method  drug1   drug2   drug3
Dynamic patient2    IV  NA  NA  56
Static  patient1    IV  NA  NA  NA
Static  patient2    IV  NA  NA  0

I noticed if I change the na to zero
df[is.na(df)]<-0
then use the ddply function it works.  But now I introduced zero when no measurement was taken.

Open to any solutions

【问题讨论】:

    标签: r grouping


    【解决方案1】:

    这是dplyr的一个选项

    library(dplyr)
    df %>% 
       group_by_at(groupColumns) %>%
       summarise_at(vars(dataColumns),  ~ if(all(is.na(.))) NA_real_ 
             else na.omit(.))
    # A tibble: 3 x 6
    # Groups:   TreatName, id [3]
    #  TreatName id       Method drug1 drug2 drug3
    #  <fct>     <fct>    <fct>  <dbl> <dbl> <dbl>
    #1 Dynamic   patient2 IV        NA    NA    56
    #2 Static    patient1 IV        34     7    NA
    #3 Static    patient2 IV        NA    NA     0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多