R – Create empty vector and append values
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 aa=c()# display itprint(a) |
Output:
NULL
A nested empty vector can also be created in R programming language.
Example:
R
# create an empty nested# vector aa=c(c(),c())# display itprint(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 aa=c()# display itprint(a)# adding numbers from 1 to# 20 to a vectora=1:20# display aprint(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 aa=c()# display itprint(a)# adding names to vector which# is emptya=c('sravan','bobby','rohith','gnnaesh','gajji')# display aprint(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 aa=c()# display itprint(a)# create an empty numeric# vector bb=c()# display itprint(b)# create an empty numeric# vector dd=c()# display itprint(d)# include numeric data into# vector a insert value 10# at location 1a[1]=10# insert value 20 at location 2a[2]=20# insert value 14.5 at location 3a[3]=14.5# insert value 89.000 at location 4a[4]=89.000# display vector aprint(a)# include logical data into vector# b at locations 1,2,3b[1]=TRUEb[2]=FALSEb[3]=FALSE# display vector bprint(b)# include character data into vector# d at locations 1,2,3d[1]="Sravan"d[2]="Bobby"d[3]="pinkey"# display vectorprint(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 aa=c()# display itprint(a)# include all type of vector # data into vector aa[1]="sravan"a[2]=20a[3]=14.5a[4]=FALSE# display vector aprint(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 aa=c()# display itprint(a)# append 10 using append()# functiona=append(a,10)# displayprint(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 aa=c()# display itprint(a)# append 10 elements from 1 to# 10 using append() functiona=append(a,c(1:10))# displayprint(a) |
Output:
NULL [1] 1 2 3 4 5 6 7 8 9 10





Please Login to comment...