0

I know there are a lot of similar-sounding questions out there, but they all relate to quite tailored and detailed processes.

My question is very basic: I want to sum 'amount' for every 'individual'.


d <- data_frame(individual = c("a","a","a","b","c","c","d","d","d","e","f","f","g"),
                amount = c("10.4","4.3","3.4","9.2","1.5","4.3","2.6","9.1","4.3","2.0","2.9","6.5","0.8"))

In my real dataset, there are thousands of individuals. Each individual will have between 1 or more values for 'amount'.

My goal is a dataframe with only one row per individual. I've tried pivot_wider to spread these multi-row 'amount' observations into new variables but the data is such that it's not really an option. And I've since been informed that a sum of the observations of 'amount' per 'individual' would be sufficient level of detail for the purpose.

kerryp
  • 35
  • 5
  • Probably `d %>% group_by(individual) %>% summarise(summ = sum(amount))` is what you need after converting `amount` to numeric. – Taufi Feb 01 '21 at 09:45
  • Turn `amount` to numeric `d$amount <- as.numeric(d$amount)` then you can do `aggregate(amount~individual, d, sum)` – Ronak Shah Feb 01 '21 at 09:45
  • 1
    Hi all - using the suggested question on https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group, I found that the following worked: ```d <- data_frame(individual = c("a","a","a","b","c","c","d","d","d","e","f","f","g"), amount = c("10.4","4.3","3.4","9.2","1.5","4.3","2.6","9.1","4.3","2.0","2.9","6.5","0.8")) d$amount = as.numeric(d$amount) e <- d %>% group_by(individual) %>% summarise(across(everything(),sum))``` which is what you just said @Taufi, thanks :) – kerryp Feb 01 '21 at 10:17

0 Answers0