I have a data frame where several columns may have the same name. In this small example, both column \"A\" and \"G\" occur twice:
A C G A G T
1 1
We can transpose dat , calculate rowsum per group (colnames of the original dat), then transpose the result back to original structure.
t(rowsum(t(dat), group = colnames(dat), na.rm = T))
# A C G T
#1 1 0 1 0
#2 4 0 6 0
#3 0 1 0 1
#4 2 0 1 0
#5 1 0 1 0
#6 0 1 0 1
#7 0 1 0 1
We could split the dataframe by name using split.default and take the row-wise sum using rowSums to create one column for each unique name.
sapply(split.default(df, names(df)), rowSums, na.rm = TRUE)
# A C G T
#1 1 0 1 0
#2 4 0 6 0
#3 0 1 0 1
#4 2 0 1 0
#5 1 0 1 0
#6 0 1 0 1
#7 0 1 0 1