Hi R users
I was wondering on how to use lapply & co when the applied function has a
conditional statement and the output is a 'growing' object.
See example below:
list1 <- list('A','B','C')
list2 <- c()
myfun <- function(x,list2)
{
one_elem <- x
cat('one_elem= ', one_elem, '\n')
random <- sample(1:2,1)
show(random)
if(random==2)
{
list2 <- c(list2,one_elem)
}else{
list2
}
}
lapply(list1,myfun,list2)
Is there a way to get rid of the 'NULL' elements in the output (when there is
any?), without using a for loop?
Thanks for your help
Lorenzo
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.