
telegram: https://teapaw.org/t.co/c0u34Ye5iZ #RStats and #Python #DataScience? Subscribe: https://teapaw.org/t.co/2H2CVje8RK…
- Tweets752
- Following286
- Followers1.3K
- Likes421
![A Carbon screenshot, showing two approaches to solving the same iterative problem in R. In one, I initialize a 0-length vector, and then, in a for loop, iteratively concatenate that vector with a new value 100,000 times. In the other, I initialize a vector with length 100,000, and then assign into it in each iteration. The first approach takes over 10 seconds, while the second takes less than a tenth of a second. The screenshotted code is pasted below: system.time({ vec1 <- numeric() for (i in 1:1e5) { vec1 <- c(vec1, i) } }) system.time({ vec2 <- numeric(1e5) for (i in 1:1e5) { vec2[i] <- i } }) all(vec1 == vec2)](https://pbs.twimg.com/media/FhJOLRqXoAEFuqq.jpg?name=small&format=webp)
ALT A Carbon screenshot, showing two approaches to solving the same iterative problem in R. In one, I initialize a 0-length vector, and then, in a for loop, iteratively concatenate that vector with a new value 100,000 times. In the other, I initialize a vector with length 100,000, and then assign into it in each iteration. The first approach takes over 10 seconds, while the second takes less than a tenth of a second. The screenshotted code is pasted below: system.time({ vec1 <- numeric() for (i in 1:1e5) { vec1 <- c(vec1, i) } }) system.time({ vec2 <- numeric(1e5) for (i in 1:1e5) { vec2[i] <- i } }) all(vec1 == vec2)



































