0

I have some transaction data for several years that I want to analyze in R. It has the following structure:

dates                  weekday  returns
1 2003-10-31           Mi        425.36
2 2003-10-31           Mi       1504.50
3 2003-10-31           Mi        170.14
4 2002-03-12           Mo       -215.80
5 2002-02-08           Mi          0.00
6 2002-04-17           Do        215.80

I want to compute the mean return per weekday.Can anyone help me?

ih17
  • 1

2 Answers2

0

With a for-loop:

means = numeric()
for(i in unique(df$weekday)){
  means[i] = mean(df[df$weekday==i,"returns"])}

Output:

> means
    Mi     Mo     Do 
 525.0 -215.8  215.8 

Obs: there are built in functions for this in dplyr.

  • Thank you. Seems to be working but I have some days with NA's instead of results. It may be due to the fact that some days dont have matching returns (Column Dates is longer than Returns one). How can I fix this issue? – ih17 Oct 31 '20 at 14:11
  • You can't run any calculation with `NA`, so you can either remove those observations (rows) from your df with `df = na.omit(df)` or keep them there but run the code without them: `mean(na.omit(df[df$weekday==i,"returns"]))` – Ricardo Semião e Castro Oct 31 '20 at 14:12
0

Does this work:

> library(dplyr)
> df %>% group_by(weekday) %>% summarise(meanreturn = mean(returns, na.rm = T))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
  weekday meanreturn
  <chr>        <dbl>
1 Do            216.
2 Mi            525 
3 Mo           -216.
> 
Karthik S
  • 11,348
  • 2
  • 11
  • 25