How to Create, Access, and Modify Vector Elements in R ?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements.

Creating a vector

It can be done in these ways:

  1. Using c() function.
  2. Using: operator.
  3. Using the seq() function.

Using C() function

The c() function in R Language is used to combine the arguments passed to it. We can create a vector by using c()

Syntax: c(value1,value2,……..,value n)

Where, c stands for combine, values are the input data to the vector.

R




# create vector with range
# operator from 1 to 20 elements
vec = c(1:20)
print(vec)


Output: 

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

So in this program, we are going to create elements from 1 to 20 and display it

Now we are going to create a vector with different data type elements and display it.

R




# create vector with different data type elements
vec=c( 1 : 20, "sravan", "deepika",
      "jyothika", 45.7, 56, 34, 56)
print(vec)


Output: 

 [1] "1"        "2"        "3"        "4"        "5"        "6"        "7"       
 [8] "8"        "9"        "10"       "11"       "12"       "13"       "14"      
[15] "15"       "16"       "17"       "18"       "19"       "20"       "sravan"  
[22] "deepika"  "jyothika" "45.7"     "56"       "34"       "56"  

Character type, float type, and int type are used. Int type data is created by using the range operator.

Using : operator

We can use : operator to creates the series of numbers in sequence for a vector. 

R




Data <- 1:20;
print(Data)


Output: 

[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

Using seq() function

seq() function is used to create a sequence of elements in a Vector. It takes the length and difference between values as optional argument. 

Syntax: seq(from, to, by, length.out)

Parameters:
from: Starting element of the sequence
to: Ending element of the sequence
by: Difference between the elements
length.out: Maximum length of the vector

R




# R Program to illustrate
# the use of seq() Function
   
# Creating vector using seq()
vec1 <- seq(1, 10, by = 2)
   
vec2 <- seq(1, 10, length.out = 7)
   
# Printing vectors
print(vec1)
print(vec2)


Output:

[1] 1 3 5 7 9
[1]  1.0  2.5  4.0  5.5  7.0  8.5 10.0

First we generates a sequence of numbers from 1 to 10 with a step size (increment) of 2. then we generates a sequence of 7 numbers from 1 to 10, evenly spaced. It calculates the step size necessary to create a sequence with the specified length.

Modifying vector elements

We can modify the vector elements by using [] operator

Syntax: vec[index_value]=new_data

Where index_value is the element index location

R




# create vector with 5 numbers
vec = c( 10, 20, 3, 4, 5)
print(vec)
 
# change 10 to 100
vec[1]=100
 
# change 20 to 200
vec[2] = 200
print(vec)


Output: 

[1] 10 20  3  4  5

[1] 100 200   3   4   5

In this example, we are going to create a vector with 5 integer type elements and modifying the 1st and 2nd elements as 100 and 200

R program to modify the entire vector at a time

R




# create vector with 20  numbers
vec = c( 1 : 20)
print(vec)
 
# modify all elements at a
# time from (1 to 20 ) - (101 to 120)
vec[1:20] = 101 : 120
print(vec)


Output:

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

 [1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

In this example, we are going to modify the elements from 1 to 20 with 1010 to 120 at a time. 

Access vector elements

We can access vector elements using the indexing operator – [] and Indexing starts from 1.

Syntax: vector_name[index_value]

R program to Access values in a vector

R




# create vector with range of
# 100 to 200  numbers
vec = c( 100 : 200 )
 
# display element 1
print(vec[1])
 
# display element 12
print(vec[12])
 
# display element 13
print(vec[13])


Output:

[1] 100

[1] 111

[1] 112

In this example, we are going to create the vector with elements from 100 to 200 using range operator and going to access the 1st, 12th and 13th elements.

Access all the elements in a vector

R




# create vector with range
# of 100 t0 200  numbers
vec = c( 100 : 200 )
 
# display all
print(vec[ 1 : 100 ])


Output:

  [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
 [21] 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
 [41] 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
 [61] 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 [81] 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

In this example, we created a vector with integer data type elements from 100 to 199, with range operator and Access all.



Similar Reads

Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
gray() or grey() function in R Language is used to create a vector of colors from a vector of gray levels. Syntax: gray(level) or grey(level) Parameters: level: a vector of desired gray levels between 0 and 1; zero indicates "black" and one indicates "white". Example 1: # R program to illustrate # gray function # Calling the gray() function gray(0)
1 min read
How to create a new vector from a given vector in R
In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Subset the VectorIf you want to create a new vector w
2 min read
Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x &lt;- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Check for the Existence of a Vector Object in R Programming - is.vector() Function
is.vector() function in R Language is used to return TRUE if the given vector is of the specified mode having no attributes other than names. It returns FALSE otherwise. Syntax: is.vector(x, mode = "any") Parameters: x: R object mode: character string naming an atomic mode or "list" or "expression" or "any" Example 1: # R program to illustrate # is
1 min read
Create, Append and Modify List in R
In R Programming Language, the list is a one dimensional data structure which can hold multiple data type elements. In this article, we are going to create a list and append data into the list and modify the list elements. Creating a list List can be created by using list() function. Syntax: list(value1,value2,.............,valuen) where values are
3 min read
Create, modify, and delete columns using dplyr package in R
In this article, we will discuss mutate function present in dplyr package in R Programming Language to create, modify, and delete columns of a dataframe. Create new columns Columns can be inserted either by appending a new column or using existing columns to evaluate a new column. By default, columns are added to the far right. Although columns can
3 min read
How to create, index and modify Data Frame in R?
In this article, we will discuss how to create a Data frame, index, and modify the data frame in the R programming language. Creating a Data Frame:A Data Frame is a two-dimensional labeled data structure. It may consist of fields/columns of different types. It simply looks like a table in SQL or like an excel worksheet. In R, to create a Data Frame
4 min read
Combine and Modify ggplot2 Legends with Ribbons and Lines
The ggplot2 library is often used for data visualization. One feature of ggplot2 is the ability to create and modify legends for plots. In this tutorial, we will cover how to combine and modify ggplot2 legends with ribbons and lines. To begin, make sure you have the ggplot2 library installed and loaded in your R Stusio. Concepts related to the topi
4 min read
Modify axis, legend, and plot labels using ggplot2 in R
In this article, we are going to see how to modify the axis labels, legend, and plot labels using ggplot2 bar plot in R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : Set the stat parameter to identify the mode.fill : Represents color inside t
5 min read
Create a Vector of Colors with specified Hue, Chroma and Luminance in R Programming - hcl() Function
hcl() function in R Language is used to create a vector of colors from vectors specifying hue, chroma and luminance. Syntax: hcl(h, c, l) Parameters: h: The hue of the colour specified as an angle in the range [0, 360]. 0 yields red, 120 yields green 240 yields blue, etc. c: The chroma of the colour. The upper bound for chroma depends on hue and lu
2 min read
Article Tags :
three90RightbarBannerImg