Date | Month | Day | Price | Volume | |
1 | 6/21/2017 | 6 | 21 | 99.43 | 32.690 |
2 | 6/22/2017 | 6 | 22 | 98.66 | 54.690 |
3 | 6/23/2017 | 6 | 23 | 97.34 | 76.690 |
4 | 6/24/2017 | 6 | 24 | 96.67 | 23.690 |
5 | 6/25/2017 | 6 | 25 | 102.95 | 72.690 |
this file name: a
question:Find the dates which yield the highest and lowest change in price from previous day?
need to use RStudio to answer it
You can run a loop from the first row, to the last but 1 row.
Store the highest difference date and lowest difference date in a variable.
Store the highest difference and lowest difference in other variables.
(Thus, we have 4 variables)
highest_date=''
lowest_date=''
highest_difference=0
lowest_difference=100000
for(row_number in 1:(dim(a)[1])-1){
price_difference= abs( a$Price[row_number +1] - a$Price[row_number ]
if(price_difference > highest_difference){
highest_difference= price_difference
highest_date= a$Date[row_number]
}
if(price_difference < lowest_difference){
lowest_difference= price_difference
lowest_date= a$Date[row_number]
}
}
Finally, highest_date and lowest_date contain the dates with the highest and lowest difference.
If you have any doubts related to the answer, or find an error in the code, please let me know and I will get back to you. Happy learning!
Get Answers For Free
Most questions answered within 1 hours.