R - Create empty vector and append values
Last Updated :
29 Jul, 2021
In this article, we will discuss how to create an empty vector and add elements into a vector in R Programming Language. An empty vector can be created by simply not passing any value while creating a regular vector using the c() function.
Syntax:
c()
This will return NULL as an output.
Example:
R
# create an empty vector a
a=c()
# display it
print(a)
Output:
NULL
A nested empty vector can also be created in R programming language.
Example:
R
# create an empty nested
# vector a
a=c(c(),c())
# display it
print(a)
Output:
NULL
Adding values to an empty vector
Method 1: Using range
We can use range (:) operator to add elements to an empty vector
Syntax:
start_value:end_value
Example:
R
# create an empty vector a
a=c()
# display it
print(a)
# adding numbers from 1 to
# 20 to a vector
a=1:20
# display a
print(a)
Output:
NULL
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Method 2: Using another vector
For this a vector is first created empty and then a vector is assigned to it.
Example:
R
# create an empty vector a
a=c()
# display it
print(a)
# adding names to vector which
# is empty
a=c('sravan','bobby','rohith','gnnaesh','gajji')
# display a
print(a)
Output:
NULL
[1] "sravan" "bobby" "rohith" "gnnaesh" "gajji"
Method 3: Using index
We can assign/fill values in an empty vector by using "[]" operator which is known as the index operator
Syntax:
vector_name[index_location]=data
where, vector_name is the name of the empty vector which is created
- Index_location is the index value where particular element is located
- Data is the value which is assigned to particular index location
Example 1:
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# create an empty numeric
# vector b
b=c()
# display it
print(b)
# create an empty numeric
# vector d
d=c()
# display it
print(d)
# include numeric data into
# vector a insert value 10
# at location 1
a[1]=10
# insert value 20 at location 2
a[2]=20
# insert value 14.5 at location 3
a[3]=14.5
# insert value 89.000 at location 4
a[4]=89.000
# display vector a
print(a)
# include logical data into vector
# b at locations 1,2,3
b[1]=TRUE
b[2]=FALSE
b[3]=FALSE
# display vector b
print(b)
# include character data into vector
# d at locations 1,2,3
d[1]="Sravan"
d[2]="Bobby"
d[3]="pinkey"
# display vector
print(d)
Output:
NULL
NULL
NULL
[1] 10.0 20.0 14.5 89.0
[1] TRUE FALSE FALSE
[1] "Sravan" "Bobby" "pinkey"
We can insert all types of vectors in one empty vector.
Example 2:
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# include all type of vector
# data into vector a
a[1]="sravan"
a[2]=20
a[3]=14.5
a[4]=FALSE
# display vector a
print(a)
Output:
NULL
[1] "sravan" "20" "14.5" "FALSE"
Method 4: Using append()
We can add data by using the append() function.
Syntax:
append(vector_name,value)
Where, vector_name is the name of the vector and value is the input value.
Example:
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# append 10 using append()
# function
a=append(a,10)
# display
print(a)
Output:
NULL
[1] 10
We can also append multiple data using c() function
Syntax:
append(vector,c(value1,value2,.value n))
Example:
R
# create an empty numeric
# vector a
a=c()
# display it
print(a)
# append 10 elements from 1 to
# 10 using append() function
a=append(a,c(1:10))
# display
print(a)
Output:
NULL
[1] 1 2 3 4 5 6 7 8 9 10
Similar Reads
How to Append Values to Vector Using Loop in R? In this article, we will discuss how to append values to a vector using a loop in R Programming Language. Appending values to an empty vector Here we are going to append the values using for loop to the empty vector. Syntax: for(iterator in range) { vector = c(vector, iterator) } where, range is th
3 min read
Remove NA Values from Vector in R In this article, we are going to discuss how to remove NA values from the vector. Method 1: Using is.na() We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. !is.na() will get the values except na. Syntax: vector[!is.na(vect
2 min read
Remove Multiple Values from Vector in R In this article, we are going to discuss how to remove multiple values from the vector in R Programming Language. We are going to remove multiple values using %in% operator Syntax: vector <- vector[! vector %in% c(elements)]Â where, vector is the input vectorelements is the values to be removed%i
2 min read
Create Comma Separated Vector in R In this article, we will discuss how to create a comma-separated vector in R Programming Language. Method 1 : Using shQuote() method First, the vector elements are enclosed with single or double quotes based on the input using the shQuote() method. This method can be used to enclose a string in doub
4 min read
How to create an array from vectors in R ? In this article, we will discuss how to create an array from the vectors in R Programming Language. We can create an array using array() function. We have to pass the vectors and dim() as parameters. Here dim() function is used to give dimensions for the array. Syntax: array_name = array( c(vector 1
2 min read
How to create an empty matrix in R ? The term empty matrix has no rows and no columns. A matrix that contains missing values has at least one row and column, as does a matrix that contains zeros. In this article, we are going to see how to create an empty matrix in R Programming Language. There are three ways of creating an empty matri
2 min read