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

后端 未结 4 491
醉酒成梦
醉酒成梦 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:03

    If you just want to remove repeated values across all columns unique() is base R

    unique(d)
    

    EDIT - Thanks for the clarification @CalumYou - I think this is what OP is looking for in base R.

    is_constant = lapply(split(d, d$study.name), function(data){
      unlist(lapply(data,function(col){
        length(unique(col)) == 1
      }))
    })
    is_constant = as.data.frame(do.call(rbind, is_constant))
    all_constant = d[,unlist(lapply(is_constant,all))]
    all_constant = unique(all_constant)
    

提交回复
热议问题