Reputation: 635
I have a data frame containing observations for a set of variables:
mydata <- data.frame(Sample = c("A", "B", "C", "D"),
V1 = c(25, 27, 18, 29),
V2 = c(32, 45, 28, 30),
V3 = c(16, 32, 29, 22),
V4 = c(27, 29, 28, 32))
I need to transform the data in each column according to a variable-specific constant E, which I have stored in another data frame:
e <- data.frame(Var = c("V1", "V3", "V2", "V4"), E = c(2, 1.4, 1.7, 1.8))
I want a new data frame containing transformed values for the data in mydata
based on the correct constant for each variable in e
, where the new value is
=1/E^(value in mydata)
I know how to use lapply
to loop over each column in mydata
, I just don't know how to specify that the value of E for each column needs to come from e
, based on the variable name. (The values in e
are not in the same order as the columns in mydata
, and in some cases there will be unused rows in e
, i.e. values of E for variables that are not present in mydata
.
How do I apply my data transformation to each column of one data frame based on the variable name and the corresponding value of E in a different data frame?
Upvotes: 1
Views: 1956
Reputation: 16026
Here's how I'd approach it. Instead of looping over the columns, I would loop over the column names:
sapply(colnames(mydata)[-1], function(x) 1/(e$E[e$Var == x]^mydata[,x]))
# V1 V2 V3 V4
# [1,] 2.980232e-08 4.223131e-08 4.591467e-03 1.281275e-07
# [2,] 7.450581e-09 4.263817e-11 2.108157e-05 3.954553e-08
# [3,] 3.814697e-06 3.527201e-07 5.784782e-05 7.118195e-08
# [4,] 1.862645e-09 1.220485e-07 6.097941e-04 6.780783e-09
Let me know if you're looking for something else.
Upvotes: 2