I am creating a time series object with new variables using the transform () function in R and cannot find the correct function to calculate the difference in the C variable between today and yesterday.
This is what I have so far:
O H L C Typical Range
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43
The next line will be added with the following function:
SPX <- transform(SPX, Return = (C - C(yesterday) ) / C(yesterday)))
Obviously, C (yesterday) is wrong. I tried lag (), diff () and did not find the right combination.
Bonus question: how do you get a typical variable to show only hundreth?
source
share