[R] A question on list and lapply
arun
smartpink111 at yahoo.com
Fri Dec 14 21:10:59 CET 2012
Hi,
If you want the list element "P" to be present as NULL in the result
you could use this:
set.seed(51)
lapply(lapply(Dat,My_Function),function(x) {if(names(Dat)[match.call()[[2]][[3]]]%in% "P") NULL else x})
A.K.
----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, December 14, 2012 1:58 PM
Subject: [R] A question on list and lapply
Dear all, let say I have following list:
Dat <- vector("list", length = 26)
names(Dat) <- LETTERS
My_Function <- function(x) return(rnorm(5))
Dat1 <- lapply(Dat, My_Function)
However I want to apply my function 'My_Function' for all elements of 'Dat' except the elements having 'names(Dat) == "P"'. Here I have specified the name "P" just for illustration however this will be some name specified by user.
Is there any direct way to achieve this, using 'lapply'?
Thanks for your help.
______________________________________________
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