Re: [R] conditioning inside an lapply

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Fri, 11 Apr 2008 23:17:29 +0100

Hi,

I have this function (largely based on this post by Thomas Lumley : http://www.opensubscriber.com/message/r-help@stat.math.ethz.ch/6984642.html)

> yapply

function(X,FUN, ...) {
  index <- seq(length.out=length(X))
  namesX <- names(X)
  if(is.null(namesX)) namesX <- rep(NA,length(X))

  FUN <- match.fun(FUN)
  fnames <- names(formals(FUN))
  if( ! "INDEX" %in% fnames ){
    formals(FUN) <- append( formals(FUN), alist(INDEX=) )   }
  if( ! "NAMES" %in% fnames ){
    formals(FUN) <- append( formals(FUN), alist(NAMES=) )   }
  mapply(FUN,X,INDEX=index, NAMES=namesX,MoreArgs=list(...)) }

> yapply(temp, function(x) x^2 )

 x y x
 4 9 16
> yapply(temp, function( x ) INDEX )

x y x
1 2 3
> yapply(temp, function( x ) NAMES )

  x y x
"x" "y" "x"

Cheers,

Romain

-----Original Message-----
From: r-help-bounces_at_r-project.org on behalf of markleeds_at_verizon.net Sent: Fri 11/04/2008 22:35
To: r-help_at_stat.math.ethz.ch; markleeds_at_verizon.net Subject: Re: [R] conditioning inside an lapply  

>From: markleeds@verizon.net
>Date: 2008/04/11 Fri PM 04:16:56 CDT
>To: r-help_at_stat.math.ethz.ch
>Subject: [R] conditioning inside an lapply

in my previous example, the last name in temp should have been z not x. i didn't intend to have duplicate names in temp. I apologize for any confusion that caused.

>This is probably basic but I want to condition based
>on the name of the component inside an lapply.
>
>So, in the simple example below, if .elem was x
>i want to do one thing but if it's y a
>different thing etc. Can someone tell me
>how to do that without using names(temp)
>as the thing one sends into the lapply ?
>Thanks.
>
>temp <- list(x=2,y=3,x=4)
>
>invisible(lapply(temp, function(.elem) {
> print(.elem)
> }))
>
>______________________________________________
>R-help_at_r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 11 Apr 2008 - 22:25:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 12 Apr 2008 - 02:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive