Skip to content

function corresponding to map of perl

1 message · Fernando Saldanha

#
I defined map as follows:

map <- function(x, y, fun) {
	mymat <- matrix( c(x,y), c(length(x), 2) )
	tmat <- t(mymat)
	oldmat <- tmat
	result <- apply(tmat, 2, function(x) {fun(x[1], x[2])})
}

It seems to work (see below). Of course you can turn it into a one-liner.
+ mymat <- matrix( c(x,y), c(length(x), 2) )
+ tmat <- t(mymat)
+ oldmat <- tmat
+ result <- apply(tmat, 2, function(x) {fun(x[1], x[2])})
+ }
[1] 5 7 9