The function you are looking for is sweep()
:
sweep(df[, -1], MARGIN = 2, div, FUN = "/")
> sweep(df[, -1], MARGIN = 2, div, FUN = "/")
V1 V2 V3
1 0.1 0.01 0.001
2 0.1 0.01 0.001
3 0.1 0.01 0.001
Hence
cbind(type = df[,1], sweep(df[, -1], 2, div, FUN = "/"))
> cbind(type = df[,1], sweep(df[, -1], 2, div, FUN = "/"))
type V1 V2 V3
1 A 0.1 0.01 0.001
2 B 0.1 0.01 0.001
3 C 0.1 0.01 0.001
gets you the desired output.
Note that here, the argument MARGIN
doesn't refer to the rows (1
) or columns (2
) like it does in apply()
. In sweep()
it refers to the margin(s) of the array that correspond to STATS
, the vector you wish to sweep out (of divide by in this case). In other words, the first element of STATS
(div
in your case) is the value to sweep out of column 1, the second element of STATS
is the value to sweep out of column 2, and so on.