Latest News

Thursday, February 28, 2019

Variables in R

                                                         VARIABLES IN R


What are Variables?:
Variables are reserved memory locations to store values.
Rules for variableName in R:
A valid variable name consists of letters, numbers and the dot or underline characters. The variable name starts with a letter or the dot not followed by a number.
Ex:
var_name%
Invalid
Has the character '%'. Only dot(.) and underscore allowed.
2var_name
invalid
Starts with a number
.2var_name
invalid
The starting dot is followed by a number making it invalid.
_var_name
invalid
Starts with _ which is not valid

Variable Assignment:
The variables can be assigned values using leftward, rightward and equal to operator.

# Assignment using equal operator.
var.1 = c(0,1,2,3)          

# Assignment using leftward operator.
var.2 <- c("learn","R")  

# Assignment using rightward operator.  
c(TRUE,1) -> var.3   

The cat() combines multiple items into a continuous print output.

var.1=c(1,2,3)
cat(var.1)
var.2<-c("learn","R")
cat(var.2)

o/p:  1 2 3learn R

Finding Variables:
1.Print(ls())  #To know all the variables currently available in the workspace
2.Print(ls(pattern=”var”)) #List the variables starting with the pattern 
 
The variables starting with dot(.) are hidden, they can be listed using "all.names = TRUE" argument to ls() function.
 
print(ls(all.names=TRUE))
Deleting Variables:
1.       rm(var_name)
All the variables can be deleted by using the rm() and ls() function together.
2.       rm(list=ls())
  • Google+
  • Pinterest
« PREV
NEXT »

No comments

Post a Comment