Array in R

Iterating an array

As in many other programming languages, you repeat an action for every entry of a vector through a for loop[1].

for(i in values){
  ... do something ...
}
samples <- c(rep(1:10))
baseList <- c(1,2,3,4,5)

XList =  list()
YList =  list()
ZList =  list()

for ( i in baseList)
{
    x = baseList[i]
    tmp = sin(x);
    XList[i] = tmp

    tmp = x*x
    YList[i] = tmp
}

seq_along creates a sequence from 1 up to the length of its input[2]:

pp <- c("Peter", "Piper", "picked", "a", "peck", "of", "pickled", "peppers")
for(i in seq_along(pp)) print(pp[i])

[1] http://www.dummies.com/programming/r/how-to-loop-through-values-in-r/
[2] https://www.safaribooksonline.com/library/view/learning-r/9781449357160/ch04.html

猜你喜欢

转载自blog.csdn.net/cocoonyang/article/details/78158610
R
R:
今日推荐