We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !
Vector in R programming is one-dimensional array to store data in which each element has the same data type. For example, names of all the family members can be stored in a vector called ‘Name’, and ages of all family members can be stored in a vector called ‘Age’, and all those people’s health status can be stored in a vector called ‘Status’. The element type of a vector can be numeric, character, or logical data.
R’s c() function denotes the meaning for ‘Combine’. Using c() is an easy way whenever you will combine or list elements manually in R. In the following example, three vectors, a, b, c of different types are created with manually inputting values for all elements.
> a <- c(1, 2, 5, 3, 6, -2, 4)
# a is a numeric vector
> b <- c("one", "two", "three")
#b is a character vector
> c <- c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)
#c is a vector of logical (boolean) data type
Note that although equal symbol = works, in most cases R programmers use specific assignment operator <- in R programming.
Note that characters in R are case-sensitive, so a and R refer to two different objects in R.
R uses sharp symbol # for comment. All the text after # in the same line are ignored by R interpreter.
2. Indexing a vector
If you want to access and refer to elements of a vector, you can use symbol bracket [ ], combined with comma, colon and c() function which have introduced.
In the following example, the 3rd element of vector a, the 1st and 3rd elements of vector b, and the 2nd, 3rd, 4th elements of vector c are returned.
> a[3]
[1] 5
> b[c(1, 3)]
[1] "one" "three"
> c[2:4]
[1] FALSE TRUE FALSE
#the returned elements can be assigned to generate a new #vector
> newb <- b[c(1,3)]
> newb
[1] "one" "three"
Note that index in R data object starts from 1, NOT 0. This machanism is different fro other programming languages, e.g. C, Python, Java, etc.
3. Create a vector of sequential numbers using seq() function
You can also use seq() function to generate a series of numbers with specified gap or items between starting and ending element. In the following example, a vector Y of 5 numbers between 1 and 10 (including those two) with equal gap, and a vector X of integer numbers 1 to 10 are generated.
#a vector of 5 numbers with equal gap between 1 and 10.
> Y <- seq(1, 10, length.out = 5)
#cat() is used to print result on screen
> cat('using seq() function', Y, '\n')
using seq() function 1 3.25 5.5 7.75 10
#seq() uses default gap 1
> X <- seq(1, 10)
> cat('using seq() function', X, '\n')
using seq() function 1 2 3 4 5 6 7 8 9 10
4. Modifying a vector
Elements of a vector can be indexed or accessed first, then assigned with new values. In the following code example, the 2nd and 3rd elements of vector X are assigned with new values. Of course, you can assign a new value to an existing vector.
#modify elements of a vector
> X[3] <- 1
> X[2] <-9
> cat('vector x is', X, '\n')
vector x is 1 9 1 4 5 6 7 8 9 10
#modify an existing vector by setting a new value to it
> X <- seq(1,20,2)
> X
[1] 1 3 5 7 9 11 13 15 17 19
5. Deleting a vector
An existing vector can be easily removed from R session by setting its new value to NULL. In the following example, vector M is created first, then deleted becasue its new value has been assigned with NULL value. You can alternatively use rm() function to remove any data objects in R.
> M <- c(8, 10, 2, 5)
# set NULL to the vector
> M <- NULL
> cat('Output vector', M)
Output vector
#alternatively, you can use rm() to remove a data object in R
> M <- c(8, 10, 2, 5)
> rm(M)
> cat('Output vector', M)
Error: object 'M' not found
For more illustrative purpose, we provide video of working with vector in R on out YouTube channel.
Click here to download Python Course Source Files !
For online Python training registration, click here ! Pandas provides flexible ways of generating data…
For online Python training registration, click here ! Data frame is the tabular data object…
Click her for course registration ! When a data frame in Python is created via…
We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…