R Vetors

Vectors are a basic data object in R. There are six types of vectors, namely logical, integer, double, complex, character and raw. You can use a c() function to create vectors, and the following shows the examples.

1. Logical Vector

data1<-c(TRUE, FALSE)
print(data1)
typeof(data1)

Output:

[1]  TRUE FALSE
[1] "logical"

2. Integer Vector

data2<-c(44L, 56L)
print(data2)
typeof(data2)

Output:

[1] 44 56
[1] "integer"

3. Double Vector

data3<-c(18.5,14.4)
print(data3)
typeof(data3)

Output:

[1] 18.5 14.4
[1] "double"

4. Complex Vector

A complex number is a combination of an actual number and an “imaginary” part.

# complex
data4<-c(2+2i, 4+5i)
print(data4)
typeof(data4)

Ouput:

[1] 2+2i 4+5i
[1] "complex"

We can check the real and imaginary parts by using Re() and Im().

# The real part
Re(data4)

# The imaginary part
Im(data4)

Output:

[1] 2 4
[1] 2 5

5. Charater Vector

data5<-c("ABC","CDE")
print(data5)
typeof(data5)

Output:

[1] "ABC" "CDE"
[1] "character"

6. Raw Vector

# raw
data6_a<-c(charToRaw('New'))
print(data6_a)
typeof(data6_a)

# raw
data6_b<-c(charToRaw('New'),charToRaw('OK'))
print(data6_b)
typeof(data6_b)

Output:

[1] 4e 65 77
[1] “raw”
[1] 4e 65 77 4f 4b
[1] “raw”


Further Reading