Thinking in Vectors with R

后端 未结 2 384
抹茶落季
抹茶落季 2021-01-31 17:55

I know that R works most efficiently with vectors and looping should be avoided. I am having a hard time teaching myself to actually write code this way. I would like some ideas

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-31 18:39

    To answer your question about why the loop of 10000 took much longer than your loop of 1000:

    I think the primary suspect is the concatenations that are happening every loop. As the data gets longer R is probably copying every element of the vector into a new vector that is one longer. Copying a small (500 elements on average) data set 1000 times is fast. Copying a larger (5000 elements on average) data set 10000 times is slower.

提交回复
热议问题