Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Variables in R

Tags:

r

I am new to the world of R Programming and when I tried declaring variables in R I couldn't find any specific way which exists In other Programming languages like C which expects a variable to be declared before using it, Though in vba we can define variables without defining it, which is assumed to be a special variant, but we can use a Special Statement called Option Explicit which does not allows us not to use the undeclared variables.

Though it is convenient way but in large Programs one can easily commit typo errors which may be extremely hard to find so my question is, In R Programming is there any such Option/Utility Exists to make the variables declared before it is defined?

like image 615
shivanesh Avatar asked Sep 02 '17 03:09

shivanesh


1 Answers

The core of R is an interpreted computer language. Which help it in declaring the variable anytime. Which is an advantage over C language where you need to declare the variables initially...But as you said "for a small programs it will be fine to define variables without declaring it initially but for a large programs we can easily commit errors by redefining the variable again".. So to overcome from this problem I have a solution....You can check every time before defining a new variable that whether it's already defined previously or not. And how you can do that see below: You can use the help of function exists()

> a <- 6
> exists("a")
[1] TRUE
> exists("b")
[1] FALSE

You can easily check whether the variable you have previously defined or not..

like image 92
Mr. Stark Avatar answered Oct 20 '22 08:10

Mr. Stark