Open In App
Related Articles

R – Create empty vector and append values

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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


Last Updated : 29 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads