rowsums() than summarizing() leads to different values than orginal column even though it should be the same

Viewed 25

this is my first post here, i've been a slilent reader for quiet a long time. I have the following problem:

I'm trying to create a summarized DF of aggregate Data, for this i need to first merge multiple column values into one for the individual observation using rowsums(). It seems like some observations get lost, i've been able to reproduce the point where the numbers start to differ, but i can't explain why.

The DF has 16459 oberservations of 138 variables containing global sales data of my company. We sell basically the same product across countries, but the data has a column for each product for each market. I use rowsums() to sum up the similar product across countries into 1 variable.

Sales_August$product1 <- rowSums(Sales_August[,10:31])

Than i use summarize() to get the aggregate data, R gives me 61627

Sales_August %>% filter(Country %in% c("DEU", "AUT", "CHE")) %>% summarise(sum(product1))

However if i only use the country specific variable, i get a slightly different value of 61591

Sales_August %>% filter(Country %in% c("DEU", "AUT", "CHE")) %>% summarise(sum(countryspecificcolumn))

Can someone explain to me why using rowsums() i use some values and how i can fix this problem?

best regards

0 Answers
Related