Cancel Preloader

Teaching R variables in simple language – A variable provides us with a named space

A variable provides us with a named space where we can write our programs. A variable in R- can be stored in a vector or group of vectors or it can be combined with many R objects. 

An allowable name for the variable, including letters, numbers, and dots or characters underlined; is. A variable name begins with a letter or a dot followed by no numbers.

Variable name Being allowed the reason

var_name2.

Allowed Includes letters, numbers, dots and underline

var_name%

illegal It is not allowed due to its existence (%), because only the dot symbol is allowed.

۲var_name

illegal Because it starts with a number.

var_name

var.name

Allowed The variable name can start with a dot (.) But should not be followed by a number.

.2var_name

illegal Following the starting point of the name is a number that makes it unauthorized.

_var_name

illegal Started with _ is not allowed

Variable allocation

Variables can be used using the left part, the right part or equal; Assigned to the operator. The values ​​of the variables can be printed using the print () function or cat (). The cat () function combines several items in the continuous print output.

# 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

print (var.1)

When the above code is executed; The following output is obtained:

[1] 0 1 2 3

var.1 is 0 1 2 3

var.2 is learn R

var.3 is 1 1

Note – Vector (c (TRUE, 1) contains a combination of logic and numeric classes. Therefore, the logic class is corrected to a numeric class and returns TRUE as 1.

One-variable data types

In the programming language R; A variable in itself; It is not express from any data type; Instead, it receives the data type of the object R to which it is assigned. So; R is called a language that is typed dynamically, which means that when using a variable in the program; Time and time again we can change the data type of the same variable.

var_x <- “Hello”

cat (“The class of var_x is“, class (var_x), ”\ n”)

var_x <- 34.5

cat (”Now the class of var_x is“, class (var_x), ”\ n”)

var_x <- 27L

cat (”Next the class of var_x becomes“, class (var_x), ”\ n”)

When we run the above code; The following result is obtain:

The class of var_x is character

Now the class of var_x is numeric

Next the class of var_x becomes integer

Find variables

To identify the variables that are currently available in the workspace; We use the Is () function. The Is () function can also use patterns to match the names of variables.

print (ls ())

When we run the above code; The following result is obtained:

[1] “my var” “my_new_var” “my_var” “var.1”

[5] “var.2” “var.3” “var.name” “var_name2.”

[9] “var_x” “varname”

Note – This is a sample output depending on the variables defined in your environment.

This Is () function can use patterns to match the names of variables.

# List the variables starting with the pattern “var”.

print (ls (pattern = “var”))

When we run the above code; The following result is obtain:

[1] “my var” “my_new_var” “my_var” “var.1”

[5] “var.2” “var.3” “var.name” “var_name2.”

[9] “var_x” “varname”

Variables starting with (.); Are hidden (Hayden); They can be listed using the “all.names = TRUE” argument to the Is () function.

print (ls (all.name = TRUE)

When we run the above code; The following result is obtain:

[1] “.cars” “.Random.seed” “.var_name” “.varname” “.varname2”

[6] “my var” “my_new_var” “my_var” “var.1” “var.2”

[11] ”var.3 ″“ var.name ”“ var_name2. ” “Var_x”

Clear variables

Variables can be clear using the rm () function. Next we have the variable var.3. We clean. An error will occur on the variable value print section.

rm (var.3)

print (var.3)

When the above code is execute; The result is as follows:

[1] “var.3”

Error in print (var.3): object ‘var.3’ not found

Can be used together with the function () rm and () Is; Clear all variables.

rm (list = ls ())

print (ls ())

When we run the above code; The following result is obtain:

character (0)

Leave a Reply

Your email address will not be published. Required fields are marked *