01. Vector Types and Declarations in R

Let's dive into how R stores and views data with its most primitive data type - vectors.


Vectors hold a certain type of values.

No declarations needed!

In strongly-typed language such as Java or C, you must declare the type of each variable (this is known as strongly typed. However, R is a loosely typed language, meaning you may assign values without declaring its type.

As shown below, there is no need to declare a vector type for either a string or numeric vector.

> x <- 4
# A numeric vector
> s <- "hello world!"
# A string vector

Checking Types

To check the types of a vector, you can use the mode() function.

> mode(x)
[1] "numeric"
> mode(s)
[1] "character"


Vectors are able to take on the values NA and NULL. Both indicate that the value is missing, but both have a clear distinction. NA indicates that the data could have some value, which is unknown. NULL, on the other hand, indicates that the data simply doesn't exist.

For example, a sample subject could have a variable "Gender" NA, meaning that it is present, but unknown. However, if there were a follow-up variable such as "Son's name," when the subject has no children, the parameter here can be NULL, indicating that the value simply does not exist.

Another important distinction is that some functions cannot be performed with an NA value. If the value is NULL, however, it treats the variable as if it didn't exist in the vector.

> x <- c(1,2,3,NA,5,6)
> mean(x)
> x <- c(1,2,3,NULL,5,6)
> mean(x)

The type of NA or NULL is dependent on the types of the other vector variables.

> x <- c("hello", "hi", NULL)
> mode(x[3])
[1] "character"
> y <- c(1, 2, 3, NA)
> mode(y[4])
[1] "numeric"

Creating a multi-valued vector

To instantiate an empty multi-valued vector, you can use the vector(length=5) function.

> x <- vector(length=5)
> x

To check the length of any existing vector using the length() function.

> length(x)
[1] 5

Instead of instantiating an empty vector, you can jump straight to assigning variables in each of its slots. Simply use the c() function (short for contatenation).

> v <- c("abc", 123)
> v
[1] "abc" "123"

Notice one thing here - all values are stored as one type. Thus, even though we inputted the numeric type 123, R converts it to the character-array "123" instead. This brings us to the point that R vectors can only have one certain type.

> mode(v)
[1] "character"

Accessing vectors by index

Each element in a vector may be accessed by its index value. Indicies start at 1, which is different from most programming languages, whose first indices start at 0.

> s <- c(1,2,3,4)
> s[1]
[1] 1
> s[3]
[1] 3
> s[4] <- 20
> s
[1] 1 2 3 20
> s[5]
[1] NA
> s[5] <- 24
[1] 1 2 3 20 24
> s[8] <- 32
[1] 1 2 3 20 24 NA NA 32

Notice that R won't error out when we attempt to assign values even with the current vector not long enough. This could either be a good thing or a bad thing, depending on whether you know exactly what you're doing.

Specifying multiple indices

You can also pull out specific indicies by using a vector. A partial listing of a vector is known as a subvector.

> x <- c(1,2,3,4,5,6)
> x[c(1,3,5)]
[1] 1 3 5

Note that you can index a value multiple times.

> x[c(1,1,1,1)]
[1] 1 1 1 1 

Excluding items

To pull out items besides specific ones, use the - key.

> x <- c(1,2,3,4,5,6)
> x[c(-3,-4)]
[1] 1 2 5 6 

Naming Vector Elements

To name the vector elements, we can use the names() function

> x <- c(97, 84, 85)
> names(x) < c("Sarah", "Mickey", "Jessica")
> x
   Sarah  Mickey Jessica
      97      84      85

Learn to be a Pythonista!

Learn Python in One Day

Learn to be a Pythonista! Try Python

Have you always wanted to learn computer programming but are afraid it'll be too difficult for you? Or you're familiar with some programming but are interested in learning Python fast? Then this book is for you. You no longer have to waste your time and money learning Python from lengthy books, expensive online courses or complicated Python tutorials.

$ Check price
11.9711.97Amazon 4 logo(185+ reviews)

More Python resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources