In the following dataset
code test1 ref1 test2 ref2
1 A 19.0 19.5 31.8 33
2 B 17.6 19.5 29.1 33
3 C 19.6 19.5 31.4 33
4 D 20.0 19.5 33.5 33
5 E 17.8 19.5 30.5 33
6 F 18.6 19.5 31.7 33
7 G 19.0 19.5 31.2 33
8 H 18.2 19.5 29.6 33
9 I 18.3 19.5 30.3 33
I want to calculate the difference between logs of the "test" and "ref" columns.
log(test1)- log(ref1) = a
log(test2)- log(ref2) = b
Then I want to calculate the mean of a and b for each row and save the result on a new column in the dataset.
I tried this function:
f <- function(x) {
x[paste0(x, sep = "_log")] <<- log(x[x])
}
sapply(names(x)[-1], f)
but I can only calculate the logs of each column and add the result to new columns.
If I continue like this, my dataset will be too long. Could anyone help me to create a code that performs all calculations and add a single column as the final result?
Many thanks in advance