> fun1 <- function(x,y){x+y}
> outer(seq(1,5,length=5),seq(6,10,length=5),fun1)
[,1] [,2] [,3] [,4] [,5]
[1,] 7 8 9 10 11
[2,] 8 9 10 11 12
[3,] 9 10 11 12 13
[4,] 10 11 12 13 14
[5,] 11 12 13 14 15
> fun2 <- function(x,y){z<-c(x,y);z[1]+z[2]}
> outer(seq(1,5,length=5),seq(6,10,length=5),fun2)
Error in dim(robj) <- c(dX, dY) :
dims [product 25] do not match the length of object [1]
fun2() क्यों काम नहीं करता है? Fun2() और fun1() अनिवार्य रूप से एक ही चीज़ नहीं हैं?बाहरी() और उपयोगकर्ता परिभाषित कार्यों के उपयोग के संबंध में सरल प्रश्न?
Details:
‘X’ and ‘Y’ must be suitable arguments for ‘FUN’. Each will be
extended by ‘rep’ to length the products of the lengths of ‘X’ and
‘Y’ before ‘FUN’ is called.
‘FUN’ is called with these two extended vectors as arguments.
Therefore, it must be a vectorized function (or the name of one),
expecting at least two arguments.
सोचो के बारे में आप क्या कर रहे हैं, तो आप एक वेक्टर में दो वैक्टर श्रृंखलाबद्ध, तो यह वेक्टर के पहले और दूसरे तत्वों का योग है: यदि आप ?outer
पढ़
नहीं, वे वह सामान नहीं है। fun1 x और y के वेक्टर योग को लौटाता है, जबकि fun2 संयुक्त x और y के पहले और दूसरे तत्वों के स्केलर योग देता है। – Andrie