[R] lapply to list of variables

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Nov 8 18:24:09 CET 2011



On 08.11.2011 17:59, Ana wrote:
> Hi
>
> Can someone help me with this?
>
> How can I apply a function to a list of variables.
>
> something like this
>
>
> listvar=list("Monday","Tuesday","Wednesday")

This is a list of length one character vectors rather than a "list of 
variables".



> func=function(x){x[which(x<=10)]=NA}

To make it work, redefine:

func <-function(x){
  x <- get(x)
  is.na(x[x<=10]) <- TRUE
  x
}



> lapply(listvar, func)
>
> were
> Monday=[213,56,345,33,34,678,444]
> Tuesday=[213,56,345,33,34,678,444]

This is not R syntax.

> ...
>
> in my case I have a neverending list of vectors.

Then your function will take an infinite amount of time - or you will 
get amazing reputation in computer sciences.

Uwe Ligges


>
> Thanks!
>
> ______________________________________________
> 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.



More information about the R-help mailing list