-1

I am new to R and have the following problem:

I have data in two columns, one column represents the earnings on a certain day and the other column contains the weekdays (encoded modulo 7, so for example Saturday is 0, Monday is 1 etc.). An example would be

 1. 12,000|6
 2. 56,000|0
 3. 25,000|1
 4. 35,000|2
 5. 18,000|3
 6. 60,000|4
 7. 90,000|5
 8. 45,000|6
 9. 70,000|0`

Are there any R commands that I can use to find out how much is earned on all Mondays, on all Tuedays and so on?

3nondatur
  • 353
  • 2
  • 9
  • Please provide data using `dput`. What are the column names in your data? What is your data called? Have you seen this https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group ? Something like this should work `aggregate(val~week, df, sum)` – Ronak Shah Jan 25 '21 at 02:14

1 Answers1

1

The general sequence here is grouping and then summarizing. In your case, you want to group by weekday and then sum all the earnings for each group. Here is an implementation of that using dplyr.

library(dplyr)

sample_data <- tibble(earnings = sample(seq(1, 10000, by=  1), replace = TRUE, 100), weekday = sample(seq(0, 6, by = 1),replace = TRUE, 100))

sample_data %>%
  group_by(weekday) %>%
  summarise(total_earnings = sum(earnings))
kkent
  • 71
  • 2