Getting started - Vectors

To create vectors, several functions are available:

Vector()

C()

X <- c(0.2 , 0.5)

X <- c(True, False)    = c(1,0)   = c(T, F)

X <- 1:30

X <- vector(“numeric”, length = 10)   => Makes an empty vector with these properties

 

Different classes of basic objects (characters, numeric, integer…) can’t be mixed properly with this function, even if no error occurs. The data will be converted to type that corresponds to any element of the vector. For example, c(5, “test”, True) will actually be recorded as [“5”, “test”, “1”], because the character type is the least common denominator. This is Implicit Coercion.

It is possible though to change the type of an object manually with a function, here are some examples:

x <- as.numeric(x)    => makes X a vector of numeric

=>  If X is not translatable to numeric, it will give a vector of NA

x <- as.logical(x)   => Makes X a vector of Boolean logical

x <- as.character(x)  => Makes X a vector of characters

Matthieu Liatard
Remember this!
Remember this
Play!
Take a quizz on this subject
Close

Hello :)
At Question-Player, we want to share more knowledge and help you never forget anything ever. And for free.

To show us your support, you can create an account:

Login / Create an account
Rate this article :
1
2
3
4
5

Up next

Related posts