I need to apply function to vector b3, but insert 0 (and do not apply function) when b3 < 0.
> b3
[1] 1.5 0.5 5.5 0.5 9.5 8.5 4.5 3.5 3.5 3.5 1.5 -0.5 1.5 5.5 9.5
[16] 5.5 2.5 1.5 2.5 3.5 9.5 -0.5 4.5 2.5 1.5 4.5 -0.5 -0.5 1.5 0.5
> unlist(lapply(b3, function(x) {seq(from = 0.5, to = x)}))
[1] 0.5 1.5 0.5 0.5 1.5 2.5 3.5 4.5 5.5 0.5 0.5 1.5 2.5 3.5 4.5
[16] 5.5 6.5 7.5 8.5 9.5 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 0.5
[31] 1.5 2.5 3.5 4.5 0.5 1.5 2.5 3.5 0.5 1.5 2.5 3.5 0.5 1.5 2.5
[46] 3.5 0.5 1.5 0.5 -0.5 0.5 1.5 0.5 1.5 2.5 3.5 4.5 5.5 0.5 1.5
[61] 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 0.5 1.5 2.5 3.5 4.5 5.5 0.5
[76] 1.5 2.5 0.5 1.5 0.5 1.5 2.5 0.5 1.5 2.5 3.5 0.5 1.5 2.5 3.5
[91] 4.5 5.5 6.5 7.5 8.5 9.5 0.5 -0.5 0.5 1.5 2.5 3.5 4.5 0.5 1.5
[106] 2.5 0.5 1.5 0.5 1.5 2.5 3.5 4.5 0.5 -0.5 0.5 -0.5 0.5 1.5 0.5
gives me close to what I need, but I have problems with negatives values of b3. So how to introduce 0 each times function get to negative value in b3 ? Here’s what I came with :
> unlist(lapply(b3, function(x) {ifelse(x>0,seq(from = 0.5, to = x),0)}))
[1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.0 0.5 0.5 0.5 0.5 0.5 0.5 0.5
[20] 0.5 0.5 0.0 0.5 0.5 0.5 0.5 0.0 0.0 0.5 0.5
works for negative values, but my function doesn’t behave as expected…
Sorry for the previous answer, I read too quickly. You probably just want to use
ifrather thanifelse: