Basics of R

<<< Click here to go back

Selecting a particular part of a vector

 # Suppose we have a vector “a”
a = 1:10

#  we just want to have the 4th item from this vector a into b
b = a[4]

# we just want to have all but 4th item from this vector a into c
c = a[-4]


#  we just want to have the 1st to 4th item from this vector a into d
d = a[1:4]

#or

d = a[c(1,2,3,4)]


#  we just want to have the all but 1st to 4th item from this vector a into e
e = a[-(1:4)]

#or

e = a[-c(1,2,3,4)]


Modifying a vector


# Suppose a have a vector name

name = c("Rajat" , "Vinod","Shobhit" , "Harish")

# Suppose we want to change the fourth element from Harish to Varun, we simply do it by

name[4] = "Varun"

# you can check it by executing

name

# Suppose we want to change the fourth element from Harish to missing value, use NA

name[4] = NA

#Now to check all the non-missing values we can Use

name[!is.na(name)]


Let's understand now, the another form of objects in R : Matrix, Data Frames and List



No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.