I have a table with 2 columns and I want to plot values from the second column against the sum of N values in the first column in the 5 rows preceding the current row.
How can I achieve this transformation in R without pre-processing externally?
Example:
File: data.csv
7,2
4,8
3,6
7,10
9,3
0,4
8,9
3,4
5,3
4,6
d = read.csv("data.csv", col.names=c("a","b"))
plot(??some_transform??(d$a), d$b)
some_transform should yield the following column:
7 = 7
7+4 = 11
7+4+3 = 14
7+4+3+7 = 21 # Up to this is optional, can start with row 5
7+4+3+7+9 = 30
4+3+7+9+0 = 23
3+7+9+0+8 = 27
7+9+0+8+3 = 27
9+0+8+3+5 = 25
0+8+3+5+4 = 20
So, the plot input is
7,2
11,8
14,6
21,10
30,3
23,4
27,9
27,4
25,3
20,6
The embed function will assemble a matrix with increasing lags from a vector. Take a look at:
So if this is intended to start working at the 5th row, then you could use these results:
EDIT: With your changed specification, add 4 zeros to the beginning of the embed argument. I am displaying the result of using that approach so it is easier to “visualize” the result of that extension to the embed first argument:
(Usually with embed (see further above) one gets a shortened array, but our questioner was willing to accept the “lead-in” phenomenon.)