GogglesSA
GogglesSA

Reputation: 75

"object not found" and "unexpected symbol" errors when timing R code with system.time()

I'm working through the book: Hands on R programming. The following code is pasted directly from the book but will not run in RStudio and I'm trying to understand why.

system.time(
output <- rep(NA, 1000000) for (i in 1:1000000) {
        output[i] <- i + 1
      }
)

I get this:

> system.time(
+     output <- rep(NA, 1000000) for (i in 1:1000000) {
Error: unexpected 'for' in:
"system.time(
    output <- rep(NA, 1000000) for"
>         output[i] <- i + 1
Error: object 'i' not found
>     }
Error: unexpected '}' in "    }"
> )
Error: unexpected ')' in ")"

Upvotes: 5

Views: 997

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73265

Firstly, a missing ;. Secondly, we need to {} all the expressions.

system.time({
output <- rep(NA, 100); for (i in 1:100) {
        output[i] <- i + 1
      }}
)

Upvotes: 6

Related Questions