0

Say I have this df:

original df

What I'd like to do is create columns that correspond to the month, where the value for the new column is the sum of the values for that person from the "Total" column. So the output would be this:

summarised df

How could I accomplish this in R? Was considering using the mutate() function or possibly:

for(i in 1:6){

nums <- df[which(df$Month == i),]
for(person in nums$Person){
per <- nums[which(nums$Person == person),]
df[[paste0('Month ', i)]] <- sum(per$Total)

}
}
colnames(df) <- c('Person', 'Year', 'Month', 1, 2,3,4,5,6)

But I feel like there's a much better way to do this.

MKN17
  • 61
  • 7

0 Answers0