———————————————————————–
# Boolean vector
z = c(TRUE, FALSE, FALSE)
z
## [1] TRUE FALSE FALSE
x=c(4,6,2.1,9)
x>3
## [1] TRUE TRUE FALSE TRUE
# Types of Vector : numeric,integer,complex, character, logical, factor
# Factor denotes the categorical variable in the data
sub=c("Phy","Chem","Chem","Sociology","Geo")
sub
## [1] "Phy" "Chem" "Chem" "Sociology" "Geo"
f1=factor(sub)
# Modification of type of a vector
gender=c("m","m","m","f","m","f")
gendFact=factor(gender)
levels(gendFact)
## [1] "f" "m"
nlevels(gendFact)
## [1] 2
# Length of a vector.
length(gender)
## [1] 6
# Length can be increased
length(gender)=8
gender
## [1] "m" "m" "m" "f" "m" "f" NA NA
gender[7]="m"
# Quiz 2: Use length function to find out the last element of a vector.
gender[length(gender)]
## [1] NA
# Numerical Operator - Vector Addition, subtraction, Multiplication,
x = c(2.1, 5, -4, 1)
y = c(0, -7, 1, 1 / 4)
x + y
## [1] 2.10 -2.00 -3.00 1.25
x*y
## [1] 0.00 -35.00 -4.00 0.25
x^y
## [1] 1.00e+00 1.28e-05 -4.00e+00 1.00e+00
# Length of the vectors are not equal.The length of the longer vector must be the multiple of the length of the shorter vector.
x=c(1,2,3,4,7,1)
y=c(2,3,1)
x+y
## [1] 3 5 4 6 10 2
# Minimum, Maximum, sum, product, cumulative sum of a vector
min(x)
## [1] 1
max(x)
## [1] 7
sum(x)
## [1] 18
prod(x)
## [1] 168
cumsum(x)
## [1] 1 3 6 10 17 18
# Logical Operator
# not, and ,or ,comparison, equality
x = c(2.1, 5, -4, 1)
x <= c(1, 6, 3, 4)
## [1] FALSE TRUE TRUE TRUE
x <= 1
## [1] FALSE FALSE TRUE TRUE
x[x <= 1]
## [1] -4 1
(x == 1 | x > 4)
## [1] FALSE TRUE FALSE TRUE
x[(x == 1 | x > 4)]
## [1] 5 1
!(x == 1 | x > 4)
## [1] TRUE FALSE TRUE FALSE
x[!(x == 1 | x > 4)]
## [1] 2.1 -4.0
# List
lst1=list(name="NSOU",nos=30000,
course=c("UG","PG","PHd"))
lst1$nos
## [1] 30000
lst1$course[3]
## [1] "PHd"
# dataframe
st=c("Rahul","Parimal","Rakhi")
age=c(24,25,24)
jtype=c("Govt","Private","Business")
df1=data.frame(st,age,jtype)
df1
## st age jtype
## 1 Rahul 24 Govt
## 2 Parimal 25 Private
## 3 Rakhi 24 Business
#Variable names of the data frame
names(df1)
## [1] "st" "age" "jtype"
#observation number of the data frame
rownames(df1)
## [1] "1" "2" "3"
#str() gives a short description about the elements (it can also be used on every R object)
str(df1)
## 'data.frame': 3 obs. of 3 variables:
## $ st : chr "Rahul" "Parimal" "Rakhi"
## $ age : num 24 25 24
## $ jtype: chr "Govt" "Private" "Business"
# Assign Car object into a new data frame
# nrow,ncol, dim, dimnames, names
# Subset of data frame
# Keep all the variables from 3rd observation
# keep the 2nd variables for all observations
# keep observations 1 to 5, 12 and 15
# remove observations 10 to 20.
# keep the last observation using nrow