Warning: session_start(): open(/tmp/sess_9ec81dcefb9a1e7876ba8f22d47ba3a7, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
assign() Function In R: Assign Values To Variables - LearnShareIT

assign() Function In R: Assign Values To Variables

assign() Function in R

This article will help you learn about the assign() function in R through the syntax and code examples. So, please read the end of this article to learn how to use this function.

What is the assign() function in R?

In the R language, the assign() function simply assigns a value to a variable name in an environment.

So, you can follow the basic syntax below.

Syntax:

assign(name, values, ...)

Parameters:

  • x: The variable name.
  • values: The value to be assigned to x.

How to use this function?

Using assign one value

The code below demonstrates how to use the assign() function to assign the value 20 to a variable named x:

# Assign 20 values to variable x
assign("x", 20)

# Print x
cat('x=',x)

Output

x= 20

Another example:

# Assign "AlexSander" value to variable "name"
assign("name", "AlexSander")

# Print name
cat('Name=',name)

Output

Name= AlexSander

Using assign vector of values

In this example, we will display how to use the assign() function to assign a vector of values to the variable vec1, vec2:

# Assign vector of values to vec1 and vec2
assign('vec1', c(8, 9, 6, 19))
assign('vec2', 20:30)

# Print vec1 and vec2
cat('vec1=',vec1,'\n')
cat('vec2=',vec2)

Output

vec1= 8 9 6 19
vec2= 20 21 22 23 24 25 26 27 28 29 30

Using assign values to variables use the paste0() function

Here, we will use the assign() function to assign values to several variables using the paste0() function and for loop. Following the code example below.

# Assign values to variables
for(i in 1:5) {
    assign(paste0("student_", i), i+5)
}

# View variables
cat('student_1=',student_1,'\n')

cat('student_2=',student_2,'\n')


cat('student_3=',student_3,'\n')


cat('student_4=',student_4,'\n')

cat('student_5=',student_5)

Output

student_1= 6 
student_2= 7 
student_3= 8 
student_4= 9 
student_5= 10

Another example:

"*2" <- function(x) {
  assign(deparse(substitute(x)), x*2, envir = .GlobalEnv)
}

# Or
"-=" <- function(x, a) {
  assign(deparse(substitute(x)), x-a, envir = .GlobalEnv)
}

# Print
# *2
c <- 2
cat('Test case 1:','\n')
while(c < 20) {
  cat(c,'')
  "*2"(c)
}

cat('\n')

# -=
cat('Test case 2:','\n')
c1 <- 20
while(c1 > 0) {
  cat(c1,'')
  "-="(c1,2)
}

Output

Test case 1:
2 4 8 16 
Test case 2:
20 18 16 14 12 10 8 6 4 2

Summary

Above are the usages of the assign() function in the R programming language. If you have any questions, please comment below. We will answer as possible.

Best of luck!

Maybe you are interested:

Posted in R

Leave a Reply

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