01. Vector Types and Declarations in R

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

Types

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"

NA and NULL

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)
NA
> x <- c(1,2,3,NULL,5,6)
> mean(x)
3.4

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
[1] FALSE FALSE FALSE FALSE FALSE

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

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Learn to be a Pythonista!

Programming Python

Learn to be a Pythonista! Try Python

Programming Python shows in-depth tutorials on the language's number of application domains including: system administration, GUIs, the Web, networking, front-end scripting layers, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

$ Check price
64.9964.99Amazon 4 logo(56+ reviews)

More Python resources

Ad