Reputation: 640
Here is a ficticious dataset consisting of 10 genetic lines and a set of 7 parameters:
data.1<-read.csv(text="
line,a,b,c,d,TF1,TF2,TF3
line1,0.716954406,0.948933475,0.723180846,0.122529549,-1,-1,-1
line2,0.443478142,0.052760906,0.814897888,0.072935389,1,-1,1
line3,0.362944986,0.892056197,0.243860823,0.197642553,1,1,1
line4,0.516451924,0.742629204,0.170810671,0.886592564,-1,-1,-1
line5,0.262766818,0.676690503,0.585481859,0.544782573,1,1,1
line6,0.437307171,0.326012372,0.194698638,0.992025701,1,1,1
line7,0.018027541,0.241761164,0.068979207,0.170437435,1,-1,1
line8,0.663364588,0.237946201,0.056954659,0.953926657,1,1,1
line9,0.062131129,0.066129381,0.156008808,0.235503941,-1,-1,-1
line10,0.018027541,0.241761164,0.06897920,0.170437435,-1,-1,-1
")
What I am trying to do is to apply the folowing function to each line and get a sim value:
flow.function <- function(a, b, c, d, TF1, TF2, TF3) {
sim <- 44.18 + 4.026*(a-12.7) + 0.195*(b-18.21) - 1.363*(c-27.4) - 0.60*(d-16.12) - 1.3*TF1 - 2.279*TF2 + 1.59*TF3
return(sim)
}
Then I apply the function row wise:
data.1$sim.results <- apply(data.1, 1, flow.function)
And this is the error I am getting:
Error in a - 12.7 : non-numeric argument to binary operator
I undertand the error is pointing out to the "12.7" value in the formula, what I have no idea why I get this error since 12.7 is a number. I am surely missing something basic.
Any help will be really appreciated.
Upvotes: 0
Views: 35
Reputation: 4233
Because you did not provide additional function arguments in apply
(it only expects one function argument). Try the following:
data.1<-read.csv(text="
line,a,b,c,d,TF1,TF2,TF3
line1,0.716954406,0.948933475,0.723180846,0.122529549,-1,-1,-1
line2,0.443478142,0.052760906,0.814897888,0.072935389,1,-1,1
line3,0.362944986,0.892056197,0.243860823,0.197642553,1,1,1
line4,0.516451924,0.742629204,0.170810671,0.886592564,-1,-1,-1
line5,0.262766818,0.676690503,0.585481859,0.544782573,1,1,1
line6,0.437307171,0.326012372,0.194698638,0.992025701,1,1,1
line7,0.018027541,0.241761164,0.068979207,0.170437435,1,-1,1
line8,0.663364588,0.237946201,0.056954659,0.953926657,1,1,1
line9,0.062131129,0.066129381,0.156008808,0.235503941,-1,-1,-1
line10,0.018027541,0.241761164,0.06897920,0.170437435,-1,-1,-1
")
flow.function <- function(a, b, c, d, TF1, TF2, TF3) {
sim <- 44.18 + 4.026*(a-12.7) + 0.195*(b-18.21) - 1.363*(c-27.4) - 0.60*(d-16.12) - 1.3*TF1 - 2.279*TF2 + 1.59*TF3
return(sim)
}
data.1$sim.results <- mapply(flow.function, data.1$a, data.1$b, data.1$c, data.1$d, data.1$TF1, data.1$TF2, data.1$TF3)
Upvotes: 1