I think data.frames are easier to work with than matrixes. I like to keep each step of the problem as a different data.frame so it is easy to retrace a step if I need to and I can see clearly what each transformation has done.
If we start with some sample data (although typically you would be reading this in from a file using read.csv
or something similar):
#get data as a data frame
df <- structure(list(v1 = c(68, 120), v2 = c(70, 121), v3 = c(71, 122), v4 = c(72, 123)), class = "data.frame", row.names = c("row1", "row2"))
df
v1 v2 v3 v4
row1 68 70 71 72
row2 120 121 122 123
Convert to log(base10):
#take log (base 10) of data frame
df_log <- log10(df)
df_log
v1 v2 v3 v4
row1 1.832509 1.845098 1.851258 1.857332
row2 2.079181 2.082785 2.086360 2.089905
Last step...
df_ans <- df_log[,] - df_log[,1]
which means, from every row and column of our log data frame df_log[,]
subtract the value from the first column df_log[,1]
and store the results in df_ans
.
Result:
df_ans
v1 v2 v3 v4
row1 0 0.012589127 0.018749436 0.02482358
row2 0 0.003604124 0.007178585 0.01072387
Note: this last operation must be done with two different data frames df_ans
and df_log
. If you tried to do it all on the same data.frame like df_log <- df_log[,] - df_log[,1]
, it won't work as df_log[,1]
will be modified to 0
mid-operation and then you would be subtracting 0
from all cells which will not work very well!