Separate columns with constant numbers and condense them to one row in R data.frame

后端 未结 4 489
醉酒成梦
醉酒成梦 2020-12-11 13:46

I have a data.frame called d. In this data.frame, some columns consist of constant numbers across the rows of the first column: study.name (see bel

4条回答
  •  无人及你
    2020-12-11 14:25

    d_list <- lapply(split(d,d$study.name), 
                     #Find columns with similar values using sapply and length(unique(cols)) 
                     #then get the 1st row
                     function(x) x[1, sapply(x,function(y) length(unique(y))==1)])
    do.call('rbind.data.frame',d_list)
    
               study.name ESL prof ESL.1 prof.1
    Bit.KnoA     Bit.KnoA   1    3     1      3
    Bit.KnoB     Bit.KnoB   1    2     1      2
    ChandlerA   ChandlerA   1    2     1      2
    Mubarak       Mubarak   2   NA     2     NA
    SheenA         SheenA   1    2     1      2
    Shin.Ellis Shin.Ellis   1    2     1      2
    Sun               Sun   2    2     2      2
    Trus.Hsu     Trus.Hsu   2    2     2      2
    

提交回复
热议问题