Is there a function in R that corresponds to the function ``map'' of perl? It could be called like: vector.a <- map( vector.b, FUN, args.for.FUN ) It should execute for each element ele.b of vector.b: FUN( vector.b, args.for.FUN) It should return a vector (or data.frame) of the results of the calls of FUN. It nearly works using: apply( data.frame( vector.b ), 1, FUN, args.for.FUN ) But when FUN is called ele.b from vector.b is no known. Thanks - Wolfram
--- In reply to: --->Date: 15.04.05 08:08 (+0200) >From: Wolfram Fischer <wolfram at fischer-zim.ch> >Subject: [R] function corresponding to map of perl > > Is there a function in R that corresponds to the > function ``map'' of perl? > > It could be called like: > vector.a <- map( vector.b, FUN, args.for.FUN ) > > It should execute for each element ele.b of vector.b: > FUN( vector.b, args.for.FUN) > > It should return a vector (or data.frame) of the results > of the calls of FUN. > > It nearly works using: > apply( data.frame( vector.b ), 1, FUN, args.for.FUN ) > But when FUN is called ele.b from vector.b is no known.Here I made a mistake. I realised now that ``apply'' does the job, e.g. apply( data.frame( 1:3 ), 1, paste, sep='', "X" ) Wolfram
I think you want 'sapply()'. -roger Wolfram Fischer wrote:> Is there a function in R that corresponds to the > function ``map'' of perl? > > It could be called like: > vector.a <- map( vector.b, FUN, args.for.FUN ) > > It should execute for each element ele.b of vector.b: > FUN( vector.b, args.for.FUN) > > It should return a vector (or data.frame) of the results > of the calls of FUN. > > It nearly works using: > apply( data.frame( vector.b ), 1, FUN, args.for.FUN ) > But when FUN is called ele.b from vector.b is no known. > > Thanks - Wolfram > > ______________________________________________ > R-help at 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 >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
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
> From: 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. > > > 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 9Maybe you're re-inventing mapply()?> mapply(mysum, a, b)[1] 5 7 9 Andy>