#Vector related R codes ##Vector
#Create Vector/variable
Age <- c(4,6,5,7,5)
Age
## [1] 4 6 5 7 5
##view variable
Age # Age of children
## [1] 4 6 5 7 5
a<-1:10
a
## [1] 1 2 3 4 5 6 7 8 9 10
b <- seq(1,4,by=0.5)
b
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0
c <- rep(1:3, times=3)
c
## [1] 1 2 3 1 2 3 1 2 3
##select position of vector
Age[4]
## [1] 7
Age[-4]
## [1] 4 6 5 5
Age[2:4]
## [1] 6 5 7
Age[-(2:4)]
## [1] 4 5
##Mathatical Sign/operatror
Age[Age==5]
## [1] 5 5
Age+5
## [1] 9 11 10 12 10
Age-2
## [1] 2 4 3 5 3
#Loop Function ##Sequence(for loop)
for (i in 1:5){
j <- i+5
print(j)
}
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
for (i in 1:5){
j <- i+5
print(j)
}
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
vote <- 170
if (vote < 18) {
print("You cannot vote because you are under 18.")
} else {
print("You can vote.")
check_vote <- function(age) {
if (age < 18) {
return("You cannot vote.")
} else {
return("You can vote.")
}
}
check_vote(17)
}
## [1] "You can vote."
## [1] "You cannot vote."
##if-else
i = -5
if(i>0){
print("non negetive")
}else{
print("zero or positive")
}
## [1] "zero or positive"
print
## function (x, ...)
## UseMethod("print")
## <bytecode: 0x000002a41994bc80>
## <environment: namespace:base>
#install.packages("tidyverse")