Let's dive into how R stores and views data with its most primitive data type - vectors.
Vectors hold a certain type of values.
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
To check the types of a vector, you can use the
> mode(x)  "numeric" > mode(s)  "character"
Vectors are able to take on the values
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
NULL is dependent on the types of the other vector variables.
> x <- c("hello", "hi", NULL) > mode(x)  "character" > y <- c(1, 2, 3, NA) > mode(y)  "numeric"
To instantiate an empty multi-valued vector, you can use the
> x <- vector(length=5) > x  FALSE FALSE FALSE FALSE FALSE
To check the length of any existing vector using the
> length(x)  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  "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)  "character"
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 > s  3 > s <- 20 > s  1 2 3 20 > s  NA > s <- 24  1 2 3 20 24 > s <- 32  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.
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 3 5
Note that you can index a value multiple times.
> x[c(1,1,1,1)]  1 1 1 1
To pull out items besides specific ones, use the
> x <- c(1,2,3,4,5,6) > x[c(-3,-4)]  1 2 5 6
To name the vector elements, we can use the
> x <- c(97, 84, 85) > names(x) < c("Sarah", "Mickey", "Jessica") > x Sarah Mickey Jessica 97 84 85
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
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