user6678274 user6678274 - 1 year ago 77
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
and the the sum of the
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?


Answer Source

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)
# tolower(name) number
#          hans     14
#         peter     16
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download