0

i am sure there is a very simple to the problem, but I am new to R and programming and I just don't see it.

i do have a data frame of the form:

countries<-c("Austria", "Austria", "USA")
date<-c("23.01.2020", "23.01.2020", "01.01.2020")
value<-c(3, 5, 4)
df<-data.frame(cbind(countries, date, value))

and what I want to do is to consolidate the rows that are doubled, same date, same country, and sum up the values for those rows.

I can do it with a loop subsetting the data frame and then doing the calculation by hand. but I am sure there is a simple end far better way to do it.

Especially as my data set is quite large and the loop takes forever. (if there is anything unclear, pls ask)

Many Thanks

Lillys
  • 23
  • 4

1 Answers1

1

First, there’s a better way to create a data.frame than cbind. You should look into doing this as a one liner x= data.frame(...)

Second, look into package dplyr. You can use the group_by and summarize commands to accomplish exactly what you want.

HEITZ
  • 136
  • 5