user6678274 user6678274 - 4 months ago 8
R Question

Data aggregation based on differently written names in R

In R I have data

Name number
Peter 14
peter 2
hans 11
...


My question is how do I find the names with Big letter and combine them small letter. For example I want to combine
peter
and
Peter
and the the sum of the
number
which in this case is 14+2=16.

Say I have over 500 names where some start with small letter and some start with Big letter - how should I solve this in general?

Thanks.

Answer

You can use aggregate() in combination with tolower(). Thus all names are converted to small characters only. The aggregation is then applied with sum. This can be stored to a new object, let's say dfAgg

df <- data.frame(name = c("Peter", "peter", "Hans", "hans"), number = c(14,2,11,3))

dfAgg <- aggregate(number ~ tolower(name), data = df, sum)
dfAgg
# tolower(name) number
#          hans     14
#         peter     16