search for: one_elem

Displaying 1 result from an estimated 1 matches for "one_elem".

Did you mean: n_elem
2011 May 05
1
lapply, if statement and concatenating to a list
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 an...