## Vector declaration in R 2

category R Programming/Data handling 2017.01.08 12:24

## How to define vector 2

In this post, we continue to study the way to define vector in various methods. R has a special command, “:”, which makes easy to describe the subcequent numbers. For example, we can define a vector manually by using c() command as before we studied.

a <- c(1,2,3,4,5,6,7,8,9,10)
a
##  [1]  1  2  3  4  5  6  7  8  9 10

Instead of doing this, we can use “:” command to define the same vector as follows:

a <- c(1:10)
a
##  [1]  1  2  3  4  5  6  7  8  9 10

Another way to define a vector is to use seq() command.

a <- seq(1, 10, 1)
a
##  [1]  1  2  3  4  5  6  7  8  9 10

Note that if you want to check the syntax of a function or command, you can simply type ?functionname in the console. In this example, let us type the following in the console.

?seq

The command seq(), which stands for “sequence”, has a pre-defined function in R whose syntax is as follows:

seq(from = Start, to = End, by = ((to - from)/(length.out - 1)),
length.out = NULL, along.with = NULL, ...)

Thus, when we type the command seq(1, 10, 1), we ask R to generate a sequence starting from 1 to 10 by step 1.

In function syntax, some arguments- by, length.out, along.with in this example- are already filled with presetting value which means users can omit the argument when they use the function except they specifically want to change the value for them. For these reason, sine the third argument of seq() function is already set as 1, we can get a same result without it:

a <- seq(1,10)
a
##  [1]  1  2  3  4  5  6  7  8  9 10

Sometimes, rep() command is also used for defining special vectors such as the zero vectors or one vectors:

a <- rep(0, 5)
a
## [1] 0 0 0 0 0

You can already guess that “rep” stands for “repeat” so that the typed command rep(0, 5) means that repeating zero for five times.

SHARE TO

#### 'R Programming > Data handling' 카테고리의 다른 글

 R에서의 벡터(Vector) 선언하기 3 - 필터링을 이용한 벡터 선언  (0) 2017.05.29 2017.05.29 2017.05.29 2017.05.28 2017.05.23 2017.05.23 2017.05.23 2017.01.08 2017.01.08 2017.01.07