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
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!) EDIT: lapply
returns a list, which we need to reform into a data frame, and finally we fix the names.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
})