R is not recognizing "While"

I am getting an error in R with R studio
Error in While(A < 10) : could not find function "While"

This is my code:

First script in R

{
A <- 1
B <- "Sukhen Dey"

While (A < 10 )

{
Print (B)
A <- A + 1
}

message("End of Job--")
}

I just installed R 3.6.2 (2019-12-12). I have reinstalled it. Same error.
Any Clues? Please help.

In R, this is lowercase while() not While()
see ?Control help page

2 Likes

Hi, and welcome!

I'll offer some much more general advice on learning R. Most users come to it with prior background in procedural/imperative programming. It's natural that we want to apply those tools to using R. And they do have a place, but it's mainly behind the scenes.

The best of what R has to offer is a richness of packages (they are case sensitive, too!) containing functions. Think school algebra f(x) = y.

One or more arguments, x produces an output y, which can be as simple as a vector of numbers or may be a very structured object capturing a wealth of results of application of f.

That's not to say that there is no programming logic in R. For example function calls on a data frame can be chained using the %>% operator, similarly to the | pipe operator. And you may find yourself using ifelse() to make binary tests and return one result on success and failure.

Therefore, the first place to look when solving a problem with R is at the off-the-shelf functions that already do that.

Good luck!

1 Like

Thanks. That works. I am not used to case-sensitivity in most languages. But that makes perfect sense.

Glad it works!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.