mrod
mrod

Reputation: 49

R sum values within two vectors

I'm learning programming with R and was given the following prompt in regards to seasonal sales:

In the new model, given two monthly periods that are otherwise identical; what is the absolute difference in predicted Elantra sales given that one period is in January and one is in March?

I am mainly working with the subset "train" in

elantra=read.csv("Week3_elantra.csv")
train=subset(elantra, Year<=2012)

I set a table in order to view the values of ElantraSales for each month (1:12), let me know if there is a better way to do this, where it returned a binary table for each ESales value

table(train$ElantraSales, train$Month)

I'm trying to tackle the original problem by summing ElantraSales values for months 1 and 3, then subtracting them to find the difference

Upvotes: 1

Views: 196

Answers (1)

D.sen
D.sen

Reputation: 922

If the goal is to return an object reflecting the total sales value for each month regardless of year than here is a potential dplyr solution.

library(dplyr)

elantra <- read.csv("Week3_elantra.csv")

elantra <- elantra %>%
  filter(Year > 2012) %>%
  group_by(Month) %>%
  summarise(sales = sum(ElantraSales))

delta <- elantra$sales[which(elantra$Month == 1)] - elantra$sales[which(elantra$Month == 3)]

If you need the total sales for each month by each year add Year before month in the group_by function. Also- ensure your Year value is numeric not character or convert using as.numericor the filter will not work properly.

Upvotes: 2

Related Questions