add a value to a vector in r

To add a value to a vector in R, you can use the c() function to concatenate the existing vector with the new value. Here's an example:

main.r
# create a vector
my_vector <- c(1, 2, 3)

# add a value to the end of the vector
my_vector <- c(my_vector, 4)

# view the updated vector
my_vector
148 chars
9 lines

Output:

main.r
[1] 1 2 3 4
12 chars
2 lines

In this example, we first create a vector called my_vector with the values 1, 2, and 3. To add a value to the end of the vector, we use the c() function to concatenate my_vector with the value 4. Finally, we view the updated my_vector with the new value using the print() function.

related categories

gistlibby LogSnag