how do I search for columns with same name, add the column values and replace these columns with same name by their sum? Using R

后端 未结 4 637
情话喂你
情话喂你 2021-01-05 20:56

I have a data frame where some consecutive columns have the same name. I need to search for these, add their values in for each row, drop one column and replace the other wi

4条回答
  •  攒了一身酷
    2021-01-05 21:16

    Some sample data.

    dfr <- data.frame(
      foo = rnorm(20),
      bar = 1:20,
      bar = runif(20),
      check.names = FALSE
    )
    

    Method: Loop over unique column names; if there is only one of that name, then selecting all columns with that nme will return a vector, but if there are duplicates it will also be a data frame. Use rowSums to sum over rows. (Duh. EDIT: Not quite as 'duh' as previously thought!) lapply returns a list, which we need to reform into a data frame, and finally we fix the names. EDIT: sapply avoids the need for the last step.

    unique_col_names <- unique(colnames(dfr))
    new_dfr <- sapply(unique_col_names, function(name)
    {
      subs <- dfr[, colnames(dfr) == name]
      if(is.data.frame(subs))
        rowSums(subs)
      else
        subs
    })
    

提交回复
热议问题