CodeInfoNet

Learn programming on the go!

Home » Create Vector In R

Create Vector In R

This article is all about creating vector and multiple ways of doing it. At this stage we all know that everything in R programming is a vector. Even a single character or a single numerical value is nothing but a vector. Let’s see how to create the vector.

In last article I have introduced you to the concept of vector. If you have missed it, you can read it here Introduction to vector in R.

There are multiple ways of creating the vector as listed below:

  1. Create vector using c()
  2. Create vector using seq()

Note: c() and seq() are the functions in R. As of now you don’t have to worry about the functions. We are going to get into functions and packages in next article.

Let’s see these functions one by one.

Using c()

Here, c() is for combine. It will combine the arguments provided to it into a vector.

Interesting part about it is it can take as many values as you want as an argument.

You can use this function to combine numbers into a vector. Also, you can combine characters into a vector using this function.

Syntax:

                c(argument1, argument2, argument3….)

Example:

                c(12, 32, 53, 44, 15)

This will combine all the values provided to it into a vector. But we need to store that vector into a variable.

Now, storing a value in variable is not a difficult task for us.

Vector1 <- c(12, 32, 53, 44, 15)

Creating a vector of characters i.e character vector is similar as creating numeric vector.

Note: The only difference here is, if you provide a numeric value into character vector, R will automatically convert it into a character but if you provide a character while creating a numeric it will not work. In fact, it will create the vector but whole numeric vector will be converted into character vector.

Let’s see how to create a character vector below,

                charVector <- c("a", "b", "c", "d", 89)

now print charVector.

You will get the result as

[1] "a"  "b"  "c"  "d"  "89"

Look how R has converted the number into a character.

Note: If you create a numeric vector as shown above, R will consider it as a double. If you want to create a vector of type integer, you need to provide a value with the postfix L to it. E.g c(2L, 4L, 20L, 19L, 98L)

 Let’s try creating vectors and check their types. Execute below code in your IDE

intVector <- c(2L, 4L, 20L, 19L, 98L)

is.Numeric(intVector)

is.Integer(intVector)

doubleVector <- c(2019, 1, 37, 10, 8)

is.double(doubleVector)

is.Numeric(doubleVector)

charVector <- c("a", "b", "c", "d", 89)

is.numeric(charVector)

is.integer(charVector)

is.double(charVector)

is.character(charVector)

Hence, we have seen the one way of creating the vector. Let’s see how we can create the vector by using another built in function i.e seq().

seq()

seq() is the abbreviation for sequence. As the name suggests, we can now guess what it can do.

The seq() function generates the sequence of numbers specified by range.

Syntax:

                seq(lowerLimit, upperLimit)

Example:

                Seq(1, 10)

Executing above line of code will give the following output.

                1  2  3  4  5  6  7  8  9 10

You might be thinking that it is the same output that we get when we execute 1:10.

Then what is the difference between writing 1:10 and using seq() function.

Executing 1:10 (it can be any number. Just for simple example we have considered it), can only give us numbers from 1 to 10. But using seq() function, you can decide how you want that sequence to be displayed.

Now, what does that mean? Let’s see:

We can define the extra parameter in seq() function. “i.e step”

We have seen 2 parameters of seq() as lowerLimit & upperLimit. The 3rd parameter says what step you want to skip in the sequence.

Have a look at below example:

exSeq <- seq(1, 10, 3)

exSeq

output: 1  4  7 10 

Notice that R has printed all the numbers between 1 & 10 but applied the step to the sequence

You can pass any parameter as a required sequence and step. Hence you don’t need to use c() function and specify each number.

Below is the code you can try out on your side.

Creating vector in R

Hence, we know how to create a vector in R programming. There are much more other ways of achieving this. We will see them in upcoming articles.

Keep me posted if you have any query. Your advice is priceless. Please provide any feedback regarding the quality of explanation or the complexity of content using below feedback form.

Feel free to comment or reach us directly via mail. Also you can contact us by Facebook, linkedin, twitter, instagram any of the platform you feel comfortable with.


[wpforms id=”22″]

Sumit Rajguru

Back to top