Generally, while doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that, when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide character, integer, floating point, double floating point, Boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
In contrast to other programming languages like C and java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable. There are many types of R-objects. The frequently used ones are −
The simplest of these objects is the vector object and there are six data types of these atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors.
| Data Type | Example |
|---|---|
| Logical | TRUE, FALSE |
| Numeric | 12.3, 5, 999 |
| Integer | 2L, 34L, 0L |
| Complex | 3 + 2i |
| Character | anything with in double quot |
| Raw | Hello is stored as 48 65 6c 6c 6f |
Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character and raw.
Even when you write just one value in R, it becomes a vector of length 1 and belongs to one of the above vector types.
# Atomic vector of type character.
print("abc");
## [1] "abc"
# Atomic vector of type double.
print(12.5)
## [1] 12.5
# Atomic vector of type integer.
print(63L)
## [1] 63
# Atomic vector of type logical.
print(TRUE)
## [1] TRUE
# Atomic vector of type complex.
print(2+3i)
## [1] 2+3i
# Atomic vector of type raw.
print(charToRaw('hello'))
## [1] 68 65 6c 6c 6f
# Creating a sequence from 5 to 13.
v <- 5:13
print(v)
## [1] 5 6 7 8 9 10 11 12 13
# Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v)
## [1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6
# If the final element specified does not belong to the sequence then it is discarded.
v <- 3.8:11.4
print(v)
## [1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8
# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by = 0.4))
## [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0
When you want to create vector with more than one element, you should use c() function which means to combine the elements into a vector. The non-character values are coerced to character type if one of the elements is a character.
# Create a vector.
apple <- c('red','green',"yellow")
print(apple)
## [1] "red" "green" "yellow"
# Get the class of the vector.
class(apple)
## [1] "character"
print(class(apple))
## [1] "character"
Elements of a Vector are accessed using indexing. The [ ] brackets are used for indexing. Indexing starts with position 1. Giving a negative value in the index drops that element from result.TRUE, FALSE or 0 and 1 can also be used for indexing.
# Accessing vector elements using position.
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]
print(u)
## [1] "Mon" "Tue" "Fri"
# Accessing vector elements using logical indexing.
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)
## [1] "Sun" "Fri"
# Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]
print(x)
## [1] "Sun" "Tue" "Wed" "Fri" "Sat"
# Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)]
print(y)
## [1] "Sun"
Two vectors of same length can be added, subtracted, multiplied or divided giving the result as a vector output.
# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)
# Vector addition.
add.result <- v1+v2
print(add.result)
## [1] 7 19 4 13 1 13
# Vector subtraction.
sub.result <- v1-v2
print(sub.result)
## [1] -1 -3 4 -3 -1 9
# Vector multiplication.
multi.result <- v1*v2
print(multi.result)
## [1] 12 88 0 40 0 22
# Vector division.
divi.result <- v1/v2
print(divi.result)
## [1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000
If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)
add.result <- v1+v2
print(add.result)
## [1] 7 19 8 16 4 22
sub.result <- v1-v2
print(sub.result)
## [1] -1 -3 0 -6 -4 0
Elements in a vector can be sorted using the sort() function.
v <- c(3,8,4,5,0,11, -9, 304)
# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result)
## [1] -9 0 3 4 5 8 11 304
# Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
## [1] 304 11 8 5 4 3 0 -9
# Sorting character vectors.
v <- c("Red","Blue","yellow","violet")
sort.result <- sort(v)
print(sort.result)
## [1] "Blue" "Red" "violet" "yellow"
# Sorting character vectors in reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
## [1] "yellow" "violet" "Red" "Blue"