Let I have an array like
a <- seq(1, 100, 1)
and I want to select just the elements that occur each 3 steps with a for()
loop starting from the second one, e.g. 2, 5, 8, 11 and so on.
How should I use for()
in this case?
b <- NULL
# for(i in 1:length(a)) { # Is there any additional argument?
# b[i] <- a[...] # Or I can just multiply 'i' by some integer?
# }
Thanks,
Use 3
as the value for by
in seq
for (i in seq(2, length(a), by=3)) {}
> seq(2, 11, 3)
[1] 2 5 8 11
Why use for
?
b <- a[seq(2,length(a),3)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With