0

I have a small dataset of daily return of stocks and I have to write a function which finds the highest return and extracts the day it occurred. Any suggestion how I do so?

This is the dataset:

> df
    VWD_RET       DATE Daily_Return
1  0.009312 2015-03-20          Fri
2 -0.001045 2015-03-23          Mon
3 -0.004701 2015-03-24          Tue
4 -0.014949 2015-03-25          Wed
5 -0.002183 2015-03-26          Thu
6  0.002501 2015-03-27          Fri
7  0.011713 2015-03-30          Mon
8 -0.007162       <NA>         <NA>
> 
JenB
  • 17,620
  • 2
  • 17
  • 45
Guy Pincus
  • 37
  • 2
  • 3
    I think the answer is there: http://stackoverflow.com/questions/743622/finding-row-index-containing-maximum-value-using-r – Thomas Baruchel Nov 28 '15 at 12:23
  • thanks, but i dont think this is the answer. actually i need to find the max value on the "VWD_RET" and to get an answer that says "Monday"(which is the highest value). i have tried to do something like that: max(df$VWD_RET, by=df$Daily_Return), but get NA as an answer.. – Guy Pincus Nov 28 '15 at 14:04

2 Answers2

2

Plenty of options to practice R syntax

Base package

df[which.max(df$VWD_RET), 3]
df[df$VWD_RET == max(df$VWD_RET), 3] # Similar to R.Schifini
df[order(-df$VWD_RET),3][1] # data frame in descending order

Output:

[1] "Mon"

dplyr

library(dplyr)
df %>% slice(which.max(VWD_RET)) %>% select(Daily_Return)

Output:

  Daily_Return
1          Mon

Ordering by VWD_RETin descending order and selecting Daily_Return.

arrange(df, desc(VWD_RET))[1, 3]

Output:

[1] "Mon"

sqldf

library(sqldf)
sqldf("select Daily_Return, max(VWD_RET) from df")[1]
sqldf("SELECT Daily_Return
      FROM df
      ORDER BY VWD_RET DESC
      LIMIT 1")
sqldf("select Daily_Return from df 
      where VWD_RET in (select max(VWD_RET) from df)")
sqldf("SELECT Daily_Return
      FROM df
      WHERE  VWD_RET=(SELECT MAX(VWD_RET) FROM df)")

Output:

  Daily_Return
1          Mon
mpalanco
  • 12,960
  • 2
  • 59
  • 67
0

Do the following:

df$Daily_Return[ df$VWD_RET == max(df$VWD_RET) ]

The expression df$VWD_RET == max(df$VWD_RET) will return a vector of TRUEs and FALSEs, being true only on those values that are equal to the maximum.

R. Schifini
  • 9,085
  • 2
  • 26
  • 32