Sunday, July 5, 2015

R programming - Assigning variables

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html

Lets learn how to assign variables in R.
In R, one can assign variables in 3 ways:
  • x = 3
  • x <- 3
  • assign("x",3)
In all the above 3 cases, the variable x is assigned the value 3.
When you type the above in R, and print x, you shall see the assignment.

One can also assign string.
  • y="test"
  • y<-"test"
  • assign("y","test")
 

How to un-assign?
To un-assign, use the command rm("variable_name")

Why un-assign?
To remove the pointer and memory allocated.



Trick:
If you want to do multiple assignments, one can do the following

x<-y<-z<-3

All variables x, y and z will have the value 3



Note:
R is a dynamically typed language. So one can keep changing data types as needed.

x<-3
x (will return 3)
x<-"test"
x (will return test)

Trick:
How to clear the console? Press CTRL+L

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html

No comments:

Post a Comment