1

I have a function like this

f(x) = a0 + 2/n * (a1 cos(x) + b1 sin(x) +  a2 cos(2*x) + b2 sin(2*x) + a3 cos(3*x) + b3 sin(3*x))

where x is a vector. I have two list one (xlist) containing the values of different x and the other the corresponding coefficients (the two lists have then the same length).

I would like to apply the f function to all the elements of xlist with the corresponding coefficients. Should I use lapply? How?

Here is an example of my data

head(list)

$`1`
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

$`2`
 [1] "6.69999980926514" "7.5"              "8.80000019073485" "8.52499973773958"
 [5] "7.96666669845581" "6.69999980926514" "8"                "8.35000002384187"
 [9] "6.86666663487752" NA                 NA                 "6.5"             
[13] "6.83749985694886" "9.25"             "7.5"              "6.90000003576279"
[17] "6.86666655540466" "6.80000003178914" NA                 "6.5"             
[21] "9.24999999999998" "7"                NA                 NA                
[25] "12.1999998092651" NA                 NA                 NA                
[29] NA                 "10.6000003814697" NA                 NA                
[33] NA                 NA                 "10.8000001907349" "10.3000001907349"
[37] NA                 "10"               NA                 NA                
[41] "9.69999980926514" NA                 "11"               "10.8000001907349"
[45] NA                 NA                 NA                 NA                
[49] NA                 NA                 NA                 "9.80000019073486"
[53] NA                 NA                 NA                 "8.80000019073486"
[57] NA                 "9.34999990463257" "9.19999980926513" "11.3666664759318"
[61] NA                 "9.23333326975504" "9.89999961853028" "9.98333326975507"
[65] "10.1000003814697" "9.89999961853028" "10.2000002861023" "10.1333335240682"
[69] "8.69999980926514" "8.10000014305114" "7.80000019073486" NA                
[73] "7.17499995231629"

$`3`
 [1] "19.2916666666667" "20.810000038147"  "20.0652381896973" "18.8437498807907"
 [5] "20.1949997901917" "20.483333407508"  "18.183333211475"  "17.5416665077209"
 [9] "17.0666666939145" "17.3499999364217" "17.7023808706374" "19.1339998626709"
[13] "18.8966665267944" "17.7916667461395" "19.3999999046326" "15.0499997933705"
[17] "15.7500001192093" "16.4111108779907" "15.1766667683919" "16.2199998378754"
[21] "13.9375"          "15.2999999523163" "15.4000000953674" "13.3000001907349"
[25] "18.6124999523163" "19.2916664282481" "16.9500002861023" "15.4666668574015"
[29] "20.3133335749308" "15.0708334048589" "18.3250002861023" "15.9000000953674"
[33] "15.5500001907349" "16.0249998569488" "19.0500001907349" "15.4000002543132"
[37] "18.4583331743876" "15.7250001430512" "18.7388887405396" "17.8388888041178"
[41] "13.0416667461396" "14.8777777883742" "18.1946297892818" "17.1666669580672"
[45] "18.5020833015442" "14.4916666348775" "14.875"           "17.1666666666667"
[49] "15.6708332697551" "19.1062924618624" "15.9388887087504" "14.6312497854233"
[53] "15.3333334128062" "17.5484848022461" "15.8914288157509" "17.9214814786558"
[57] "15.3875000476837" "18.739999961853"  "15.3555357796805" "18.3854166666667"
[61] "19.3625001907349" "19.6166665395101" "18.7305555873447" "19.2566665331522"
[65] "19.1083335876465" "17.6285716465541" "18.8063492396521" "16.6624999046326"
[69] "18.3625003099441" "18.8749999523163" "18.389999961853"  "17.3500001430512"
[73] "15.0333334604899"

head(coefflist)

$`1`
[1] -0.99816132  0.94322618  1.20707782  0.18038590  0.40502377 -0.06045413
[7]  0.16397336

$`2`
[1] -1.03218220  0.86101832  1.22405685  0.17861695  0.48982884 -0.00870947
[7]  0.07130413

$`3`
[1] -0.76477491  0.81255792  1.06977327  0.08542454  0.48862804  0.10553910
[7]  0.16869875


length(list)
73
head(coefflist)
73

Many thanks

4
  • Could you give us some example of your data? Commented Feb 17, 2014 at 16:43
  • 3
    I think you want ?mapply Commented Feb 17, 2014 at 16:46
  • Please can you provide an example of your data. As the question is currently written, it is difficult to see what form your data takes. See this question stackoverflow.com/q/5963269/134830 for how to provide your data to us. Commented Feb 17, 2014 at 16:46
  • Yes sorry, n is constant Commented Feb 17, 2014 at 17:45

2 Answers 2

3

I don't use mapply much, so here is an example I walked through myself:

set.seed(1618)
coeflist <- list(a = rnorm(5),
                 b = runif(5),
                 x = rnorm(5),
                 n = seq(1, 500, length = 5),
                 x.all = rnorm(100))


mapply(function(a, b, x, n) a + (2 / n) * (a * cos(x) + b * sin(x) +
                                             a * cos(2 * x) + b * sin(2 * x) + 
                                             a * cos(3 * x) + b * sin(3 * x)),
       coeflist[['a']], coeflist[['b']], coeflist[['x']], coeflist[['n']])

# [1] -1.0978364 -0.4403969 -0.5748082  0.5659216 -1.7714444

Below is the long way to get mapply results

# super long way (take the first element in each list and apply the function);
# repeat for each of n elements in list:
sapply(1:5, function(qq)

  coeflist[['a']][qq] + (2 / coeflist[['n']][qq]) * 
    (coeflist[['a']][qq] * cos(coeflist[['x']][qq]) +
       coeflist[['b']][qq] * sin(coeflist[['x']][qq]) +
       coeflist[['a']][qq] * cos(2 * coeflist[['x']][qq]) + 
       coeflist[['b']][qq] * sin(2 * coeflist[['x']][qq]) +
       coeflist[['a']][qq] * cos(3 * coeflist[['x']][qq]) +
       coeflist[['b']][qq] * sin(3 * coeflist[['x']][qq]))
)
# results are same as above but with much more work:
# [1] -1.0978364 -0.4403969 -0.5748082  0.5659216 -1.7714444

And this is if you have a lot of x's and want to run each one for each set of coefficients

 sapply(coeflist[['x.all']], function(x) 
   mapply(function(a, b, x, n) a + (2 / n) * (a * cos(x) + b * sin(x) +
                                                a * cos(2 * x) + b * sin(2 * x) +
                                                a * cos(3 * x) + b * sin(3 * x)),
          coeflist[['a']], coeflist[['b']], x, coeflist[['n']])
 )

#           [,1]         [,2]       [,3]       [,4]       [,5]       [,6]       [,7]
# [1,]  3.5548544 -0.08107256 -0.5391973  0.4732270 -3.7242500 -5.9636420 -5.9918891
# [2,] -0.3915859 -0.40732538 -0.4114718 -0.4048558 -0.4259481 -0.4343241 -0.4344591
# [3,] -0.5550217 -0.56512808 -0.5565757 -0.5639058 -0.5623012 -0.5745126 -0.5745335
# [4,]  0.5654853  0.57204328  0.5618304  0.5714012  0.5640612  0.5748265  0.5748127
# [5,] -1.7533459 -1.76927445 -1.7559892 -1.7673417 -1.7650747 -1.7842027 -1.7842367

etc, etc. I have 100 results, one for each x in x.all in my list, and for each of those 100, there are 5 calculations, one for each combination of coefficients: a, b, n, and x.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for your answer but what I was looking for is a method to associate the i element of xlist (xlist[[i]]) with the set of coefficient (coeflist[[i]]) and than calulate f[i].
R keeps things in order and is vectorized.. so just stick [i] on mapply such as mapply(...)[i] to get the calculation for a[i], b[i], n[i], and x[i].
And since n is a constant, change the arg list from coeflist[['n']] to n = 5
3

Working with your data:

list = structure(list(`1` = structure(list(V1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = "V1", class = "data.frame", row.names = c(NA, -73L)), `2` = structure(list(V1 = c(6.69999980926514, 7.5, 8.80000019073485, 8.52499973773958, 7.96666669845581, 6.69999980926514, 8, 8.35000002384187, 6.86666663487752, NA, NA, 6.5, 6.83749985694886, 9.25, 7.5, 6.90000003576279, 6.86666655540466, 6.80000003178914, NA, 6.5, 9.24999999999998, 7, NA, NA, 12.1999998092651, NA, NA, NA, NA, 10.6000003814697, NA, NA, NA, NA, 10.8000001907349, 10.3000001907349, NA, 10, NA, NA, 9.69999980926514, NA, 11, 10.8000001907349, NA, NA, NA, NA, NA, NA, NA, 9.80000019073486, NA, NA, NA, 8.80000019073486, NA, 9.34999990463257, 9.19999980926513, 11.3666664759318, NA, 9.23333326975504, 9.89999961853028, 9.98333326975507, 10.1000003814697, 9.89999961853028, 10.2000002861023, 10.1333335240682, 8.69999980926514, 8.10000014305114, 7.80000019073486, NA, 7.17499995231629)), .Names = "V1", class = "data.frame", row.names = c(NA, -73L)), `3` = structure(list(V1 = c(19.2916666666667, 20.810000038147, 20.0652381896973, 18.8437498807907, 20.1949997901917, 20.483333407508, 18.183333211475, 17.5416665077209, 17.0666666939145, 17.3499999364217, 17.7023808706374, 19.1339998626709, 18.8966665267944, 17.7916667461395, 19.3999999046326, 15.0499997933705, 15.7500001192093, 16.4111108779907, 15.1766667683919, 16.2199998378754, 13.9375, 15.2999999523163, 15.4000000953674, 13.3000001907349, 18.6124999523163, 19.2916664282481, 16.9500002861023, 15.4666668574015, 20.3133335749308, 15.0708334048589, 18.3250002861023, 15.9000000953674, 15.5500001907349, 16.0249998569488, 19.0500001907349, 15.4000002543132, 18.4583331743876, 15.7250001430512, 18.7388887405396, 17.8388888041178, 13.0416667461396, 14.8777777883742, 18.1946297892818, 17.1666669580672, 18.5020833015442, 14.4916666348775, 14.875, 17.1666666666667, 15.6708332697551, 19.1062924618624, 15.9388887087504, 14.6312497854233, 15.3333334128062, 17.5484848022461, 15.8914288157509, 17.9214814786558, 15.3875000476837, 18.739999961853, 15.3555357796805, 18.3854166666667, 19.3625001907349, 19.6166665395101, 18.7305555873447, 19.2566665331522, 19.1083335876465, 17.6285716465541, 18.8063492396521, 16.6624999046326, 18.3625003099441, 18.8749999523163, 18.389999961853, 17.3500001430512, 15.0333334604899)), .Names = "V1", class = "data.frame", row.names = c(NA, -73L))), .Names = c("1", "2", "3"))
coefflist = structure(list(`1` = structure(list(V1 = c(-0.99816132, 0.94322618, 1.20707782, 0.1803859, 0.40502377, -0.06045413, 0.16397336)), .Names = "V1", class = "data.frame", row.names = c(NA, -7L)), `2` = structure(list(V1 = c(-1.0321822, 0.86101832, 1.22405685, 0.17861695, 0.48982884, -0.00870947, 0.07130413)), .Names = "V1", class = "data.frame", row.names = c(NA, -7L)), `3` = structure(list(V1 = c(-0.76477491, 0.81255792, 1.06977327, 0.08542454, 0.48862804, 0.1055391, 0.16869875)), .Names = "V1", class = "data.frame", row.names = c(NA, -7L))), .Names = c("1", "2", "3"))

f <- function(x, coef, n) {
  coef=coef$V1
  x=x$V1
  a0 = coef[1]
  a1 = coef[2]
  a2 = coef[3]
  a3 = coef[4]
  b1 = coef[5]
  b2 = coef[6]
  b3 = coef[7]  
  a0 + 2/n * (a1*cos(x) + b1*sin(x) +  a2*cos(2*x) + b2*sin(2*x) + a3*cos(3*x) + b3*sin(3*x))
}
y = mapply(f, x=list, coef=coefflist, n=1)
head(y)

#       1          2         3
# [1,] NA  2.8195891  3.010415
# [2,] NA -1.7689234 -2.123251
# [3,] NA -0.8235252 -1.080907
# [4,] NA -1.4899560  3.156538
# [5,] NA -2.6507462 -1.653132
# [6,] NA  2.8195891 -2.340157

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.