In a typical spreadsheet I am able to create a formula that references a cell on any row.
So let us say I have the following data:
a=1:10
b=11:20
c=data.frame(a,b)
Which would yield this:
a b
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
10 10 20
So in a spreadsheet I can place in what would be cell c2 the absolute formula c$b2*c$a1 and get the number 12. Or maybe even something more complex like ifelse(c$a>5,c$b*c$a[-1]*2,c$b*c$a[-1] using relative references.
a b c
1 1 11 NA
2 2 12 12
3 3 13 26
4 4 14 42
5 5 15 60
6 6 16 80
7 7 17 204
8 8 18 252
9 9 19 304
10 10 20 360
I know R is not a spreadsheet, but is there a package or function that would allow me to treat data frames in such a manner or is there something fundamentally wrong with that kind of functionality? I keep having these issues with functions that require the spanning of more than one record or row. Almost all the stuff I work with is a time-series if that helps at all. Is there a document anywhere that explains such functions? Maybe there is a ?topic I missed somewhere.
You were close, you need to complete the offset by excluding the first and last values, then wrap all that in
ifelse:(I choose
das a variable name sincecis a very commonly used function.)For a tidier answer keep the main calculation out of the
ifelseand use it only for the multiplier: