Is there some function in R of the form like:
crossApply(v1, v2, func)
that has the same functionality as:
ret = c()
i = 1
for (e1 in v1) {
for (e2 in v2) {
ret[i] <- func(e1,e2)
i <- i + 1
}
}
return(ret)
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think you may be looking for
outerwhich isn’t exactly what your code does, but it’s close. Specifically,outerwill return the matrix (i.e. the outer product) or each combination of the elements of its first two arguments.You’d probably want to store the result and then extract the lower triangle as a vector. Something like this maybe: