create vector in r

R Create Vector

Posted on

How To Create Vector in R?

There are three ways to create a vector in R programming:
using c() function, using seq() function and using : operator.

Create a vector using c() function

To create vector using c() is to combine values together.

Example

m1 <- c(1, 3, 5, 100, 21)
m2 <- c(1.3, 3.5, "5.6", "a", -1)
cat(m1)
cat(m2)

#output:
1 3 5 100 21
1.3 3.5 5.6 a -1

Create a vector using seq() function

Creating vector using seq() is to make a vector of sequence values.

Example:

m <- seq(0, 30, length.out=6)
cat(m)

#output:
0 6 12 18 24 30

Create a vector using : operator

We use this : operator to create a vector with continuous values.

Example:

m1 <- 1:5
m2 <- 1.2:5
m3 <- 1:5.2
m4 <- 1.2:5.2
cat(m1)
cat(m2)
cat(m3)
cat(m4)

#output:
1 2 3 4 5
1.2 2.2 3.2 4.2
1 2 3 4 5
1.2 2.2 3.2 4.2 5.2

Conclusion

in R programming, we could create a vector using c() function, seq() function and : operator.