0

I have a dataset with 2 calendar variables (Week & Hour) and 1 Amount variable:

 Week Hour Amount
   35    1    367
   35    2    912
   36    1    813
   36    2    482
   37    1    112
   37    2    155
   35    1    182
   35    2    912
   36    1    551
   36    2    928
   37    1    125
   37    2    676

I wish to replace each value of Amount with the mean from each observation with the same Week/Hour pair. For instance, here there are 2 obs. for (Week=35, Hour=1), with Amount values of 367 and 182. Hence, for this example, the 2 rows with (Week=35, Hour=1) should have the Amount replaced with mean(c(367,182). The final output should be:

Week Hour Amount
  35    1  274.5
  35    2  912.0
  36    1  682.0
  36    2  705.0
  37    1  118.5
  37    2  415.5
  35    1  274.5
  35    2  912.0
  36    1  682.0
  36    2  705.0
  37    1  118.5
  37    2  415.5

I have the following code that solves this issue. However, for the complete dataset with thousands of rows, it is very slow. Is there any way to automatically reshape with with this paired means?

dataset = data.frame(Week=c(35,35,36,36,37,37,35,35,36,36,37,37),
                     Hour = c(1,2,1,2,1,2,1,2,1,2,1,2),
                     Amount = c(367,912,813,482,112,155,182,912,551,928,125,676))

means <- reshape2::dcast(dataset, Week~Hour, value.var="Value", mean)

for (i in 1:nrow(dataset)) {
  print(i)
  dataset$Amount[i] <- means[means$Week==dataset$Week[i],which(colnames(means)==dataset$Hour[i])]
}

3 Answers 3

3

Possible solution with dplyr:

dataset %>% 
  group_by(Week, Hour) %>% 
  summarise(mean_amount = mean(Amount))

You group by Week and Hour and calculate the mean based on this condition.

EDIT

To keep the original structure (number of rows) alter the code to

dataset %>% 
  group_by(Week, Hour) %>% 
  mutate(Amount = mean(Amount))
2
  • 1
    But that's not really what the OP wants. May 7, 2020 at 11:07
  • Edited, now it should be what OP was asking for.
    – Nico
    May 7, 2020 at 11:26
1

If the idea is just to get the mean Amount by Week and Hour, this would work:

aggregate(Amount ~ ., dataset, mean)
  Week Hour Amount
1   35    1  274.5
2   36    1  682.0
3   37    1  118.5
4   35    2  912.0
5   36    2  705.0
6   37    2  415.5

EDIT:

If, however, the idea is to put the averages back into the dataset, then this should work:

x <- aggregate(Amount ~ ., dataset, mean)
dataset$Amount <- x$Amount[match(apply(dataset[,1:2], 1, paste0, collapse = " "), 
                                 apply(x[,1:2], 1, paste0, collapse = " "))]
dataset
   Week Hour Amount
1    35    1  274.5
2    35    2  912.0
3    36    1  682.0
4    36    2  705.0
5    37    1  118.5
6    37    2  415.5
7    35    1  274.5
8    35    2  912.0
9    36    1  682.0
10   36    2  705.0
11   37    1  118.5
12   37    2  415.5

Explanation:

This pastes together into strings the rows of the first two columns in the means dataframe x and in datasetusing the function apply it uses match on these strings to assign the means values to the corresponding rows in dataset

EDIT 2:

Alternatively, you can use interaction and, respectively, %in% for this transformation:

dataset$Amount <- x$Amount[match(interaction(dataset[,1:2]), interaction(x[,1:2]))]
# or:
dataset$Amount <- x$Amount[interaction(x[,1:2]) %in% interaction(dataset[,1:2])]
1
0

Base R solution:

dataset$Amount <- with(dataset, ave(dataset$Amount, dataset$Week, dataset$Hour, FUN = mean))

Data:

dataset = data.frame(Week=c(35,35,36,36,37,37,35,35,36,36,37,37),
                     Hour = c(1,2,1,2,1,2,1,2,1,2,1,2),
                     Amount = c(367,912,813,482,112,155,182,912,551,928,125,676))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.