2014-02-17 85 views
1

我有這樣應用功能列表ř

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 

非常感謝

+0

你能不能給我們你的數據的一些例子嗎? – Baumann

+3

我想你想''mapply' –

+0

請你可以提供一個你的數據的例子。由於這個問題目前已經寫好,所以很難看到你的數據是以什麼形式出現的。看到這個問題http://stackoverflow.com/q/5963269/134830如何向我們提供您的數據。 –

回答

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。

+0

嗨,感謝您的回答,但是我正在尋找的是一種將xlist(xlist [[i]])的i元素與係數集(coeflist [[i]]關聯起來的方法)而不是計算f [i]。 – user3036416

+0

R保持順序並向量化..所以只需將'[i]'粘貼在'mapply'上,例如'mapply(...)[i]'來計算[i],b [i] ,n [i]和x [i]。 – rawr

+0

由於n是一個常量,因此將arg列表從'coeflist [['n']]'更改爲'n = 5' – rawr

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