Re: [R] function corresponding to map of perl

From: Fernando Saldanha <fsaldan1_at_gmail.com>
Date: Sat 16 Apr 2005 - 22:44:35 EST


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.

> a<-c(1,2,3)
> b<-c(4,5,6)
> mysum <- function(x, y) {x + y}
> 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])})
+ }

> (test <- map(a, b, mysum))

[1] 5 7 9

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Apr 16 22:53:30 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:12 EST