x<-8 # Assign value 8 to variable object x
print(x)
## [1] 8
class(x) # To display the class or data type of x.
## [1] "numeric"
y<-c(10,12,13,15) # "c" is concatenate which is used for concatenating set of one-element vectors.
y
## [1] 10 12 13 15
Note that all elements are of same type.
y<-c(10,12,13,15)
y[1] #Accessing first element of the vector.
## [1] 10
Note that in R language, the indexing starts from 1 and not 0(unlike python and c).
y<-c(10,12,13,15)
y[2:4]
## [1] 12 13 15
y<-c(10,12,13,15)
y[c(1,3)]
## [1] 10 13
y<-c(10,12,13,15)
y[c(1,1,3)]
## [1] 10 10 13
Negative subscripts mean that we want to exclude the given elements in our output.
y<-c(10,12,13,15)
y[-2]
## [1] 10 13 15
y<-c(10,12,13,15)
y[-1:-3]
## [1] 15
y<-c(10,12,13,15)
length(y)
## [1] 4
y<-c(10,12,13,15)
y[-length(y)] # Here the length of y is 4. i.e y[-4]. So, we are removing last element from the vector y.
## [1] 10 12 13
x<-vector(length = 2) # Specify what data type and its length
x[1]<-44
x[2]<-33
x
## [1] 44 33
z<-3:10 # Generate numbers in the range.
z
## [1] 3 4 5 6 7 8 9 10
z<- seq(from=4,to=16, by=2) # Generates numbers from 4 to 16 with intervel of 2
z
## [1] 4 6 8 10 12 14 16
z<- seq(from=10, to=20, length=3) # 3 elements are generated in the range 10 to 20 with equal interval
z
## [1] 10 15 20
z<- rep(8,4) # Repeat number 8 4 times.
z
## [1] 8 8 8 8
z<-rep(c(5,6,7),each=2) # each element in the vector is repeated 2 times
z
## [1] 5 5 6 6 7 7
When applying an operation to two vectors that requires them to be the same length, R automatically recycles, or repeats, the shorter one, until it is long enough to match the longer one.
x<-c(1,2,3,4)
y<-c(5,6,7,8,9,10)
x+y
## Warning in x + y: longer object length is not a multiple of shorter object
## length
## [1] 6 8 10 12 10 12
The any() and all() functions are handy shortcuts. They report whether any or all of their arguments are TRUE.
z<- c(1,3,5,6,7) # Returns true, if any number in vector z are greater than 3.
any(z>3)
## [1] TRUE
z<- c(1,3,5,6,7)
all(z>3) # Returns true, if all elements in vector z are greater than 3.
## [1] FALSE
In statistical data sets, we often encounter missing data, which we represent in R with the value NA. NULL, on the other hand, represents that the value in question simply does not exist, rather than being existent but unknown.
x<-c(1,3,6,5,7,NA,6) # This vector contains one NA
mean(x) # If you try to caliculate mean of x, you will get NA. Which is not desired.
## [1] NA
Remove NA to calculate the mean
mean(x, na.rm = TRUE)
## [1] 4.666667
let’s calculate length of NA
p<-NA
length(p) # The answer is 1
## [1] 1
Now, Calculate the length of NULL
p<-NULL
length(p)# The answer is 0- Null means, it does not exists.
## [1] 0
Practically, Null is used to initialize a variable to zero.
This allows us to extract a vector’s elements that satisfy certain conditions.
z<-c(1,3,4,7,9) # Define a vector
j<-z*z>8 # calculate square of every element and find if they are greater than 8.
print(j) # The output of j is in the form of logical-TRUE and FALSE
## [1] FALSE TRUE TRUE TRUE TRUE
z[j]# Displays the numbers whose square is greater than 8
## [1] 3 4 7 9
z<-c(1,3,4,7,9)
which(z*z>8) # Returns the indexes of elements following the condition.
## [1] 2 3 4 5