When using for example a for loop I can use next
to skip a certain item:
if (i == 5) next
print (i)
Is there a similar way to do this in a map() or more specifically in a pmap()?
a <- c(1,2,3)
b <- c(1,1,1)
c <- c(2,2,2)
mapped <- pmap(list(a,b,c),
function(a,b,c){
# if (a == 2) next
print( a + b - c)
})
Thank you for your help!
########## Edit for formatting of my follow up question to Konrad:
Is there any way to avoid getting a NULL element in the list when using if (a != 2)
?
a <- c(1,2,3)
b <- c(1,1,1)
c <- c(2,2,2)
mapped <- pmap(list(a,b,c),
function(a,b,c){
if(a != 2){
a + b - c
}
})
str(mapped)
returns
#List of 3
#$ : num 0
#$ : NULL
#$ : num 2
But for my solution I would need
#List of 2
#$ num 0
#$ num 2