I have a function taking four arguments,
h(a, b, c, d)
Where a
and b
are the i-th and the i+1-th row of df1
and c
and d
are the i-th and i+1-th row of df2
, and the output has four variables and i-1 results.
The idea is the following: I want to use the function h
to each combination of these four arguments where i
is common, and so:
- for the first iteration it will take the 1st and 2nd row of df1
and 1st and 2nd row of df2
- for the second iteration it will take the 2nd and 3rd row of df1
and 2nd and 3rd row of df2
...
Afterward, perfectly, the results will be stored in a separate data frame, with 4 columns and i-1 rows.
I tried making use of apply
function and of a for
loop, yet my attempts failed me. I don't necessarily need a readymade solution, a hint would be nice. Thanks!
EDIT: reproducible example:
df1 <- data.frame(a = c(1, 2, 3, 4), b = c(5, 6, 7, 8))
df2 <- data.frame(c = c(4, 3, 2, 1), d = c(8, 7, 6, 5))
h <- function (a, b, c, d) {
vector <- (a + b) / (c - d)
vector
}
I would like to get a function that uses h
until b
and d
reach the last row of df1
/df2
(they have the same number of rows), and for each such combination generate vector
and add it to some new data frame as a next row.