I want to use the results of the sequential function as an input in the for loop in R, how can I do that? Please see the example below.
x1<-seq(1,100,10)
for (k in x1 ) {
L1<-
L2<- }
The L1
will be 1 and L2
will be 11 for the first loop and L1
will be 11 and L2
will be 21 for the second loop. Thanks
You can loop over the index of x1
:
for (k in seq_along(x1[-1])) {
L1<- x1[k]
L2<- x1[k+1]
}
seq_along
creates a sequence from 1:length(x1)
seq_along(x1)
#[1] 1 2 3 4 5 6 7 8 9 10
We use -1
because we want the loop to stop at the second last place as there is no k + 1
for last value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.