将功能应用于列表R.

时间:2014-02-17 16:40:32

标签: r

我有这样的功能

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))

其中x是向量。我有两个列表一(xlist)包含不同x的值,另一个列出相应的系数(两个列表的长度相同)。

我想将f函数应用于xlist的所有元素以及相应的系数。我应该使用lapply吗?怎么样?

以下是我的数据示例

头(列表)

$`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"

头(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

非常感谢

2 个答案:

答案 0 :(得分:3)

我没有多使用mapply,所以这是我自己走过的一个例子:

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

以下是获得mapply结果

的漫长道路
# 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

这就是你有很多x并希望为每组系数运行每一个

 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

等等我有100个结果,在我的列表中x.all中每个x一个,并且对于这100个中的每一个,有5个计算,每个系数组合一个:a,b,n和x。

答案 1 :(得分:3)

使用您的数据:

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