Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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 ")"
like image 453
GogglesSA Avatar asked Jul 26 '18 10:07

GogglesSA


1 Answers

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
      }}
)
like image 182
Zheyuan Li Avatar answered Sep 24 '22 00:09

Zheyuan Li