0

i have a 3 x 8 Data Frame, and i need to extract the day (column 3) that matches the highest value (column 1) from the Data Frame.

For example,

1    0.931 2015-03-20    Friday
2   -0.105 2015-03-23    Monday
3   -0.470 2015-03-24   Tuesday
4   -0.149 2015-03-25 Wednesday

the highest value is 0.931, so i need to extract the value Friday.

Thanks.

nghauran
  • 6,648
  • 2
  • 20
  • 29
  • 1
    Try `df[which.max(df$value), 3]` – nghauran Nov 09 '17 at 16:58
  • response is character (0), column #3 isnt defined as vector. it was created through a command that create a column that indicates the day of a certain date. DF_sec5$TrdDate_Weekday <- weekdays(as.Date(DF_sec5$TrdDate)) , for this df > DF_sec5 VWRet_DF TrdDate_DF NA 1 0.931 2015-03-20 Friday 2 -0.105 2015-03-23 Monday 3 -0.470 2015-03-24 Tuesday 4 -0.149 2015-03-25 Wednesday 5 -0.218 2015-03-26 Thursday 6 0.250 2015-03-27 Friday 7 1.171 2015-03-30 Monday 8 -7.162 2015-03-31 Tuesday – Eliad Harell Nov 09 '17 at 17:06
  • 1
    Posting your data helps others help you. For example, if your data frame is named `df`, you can give us your data by posting the output of `dput(df)` in your question. Read more about creating a minimal reproducible example [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – duckmayr Nov 09 '17 at 17:22

1 Answers1

1

Say, your dataframe (df) has the values in column named V1 then you can find the index of the maximum value in this column with which.max(df$V1). You say you want the corresponding day for this value, say the days are in column named V2 then your day is df$V2[which.max(df$V1)].

T C Molenaar
  • 3,205
  • 1
  • 10
  • 26