This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# This is comment in R
x <- 10
x
## [1] 10
y <- "10"
y
## [1] "10"
# This is vector of gender
gender <- c("F", "M", "F", "M", "F")
#Now if you want to stop to see all objects created thus far.
ls()
## [1] "gender" "x" "y"
#Create an object of values between 2 and 9
myNum <- 2:9
myNum
## [1] 2 3 4 5 6 7 8 9
#Create an age vector
age <- c(20, 24, 19)
age
## [1] 20 24 19
#We can do calculations and algebra as well.
# sum
1+1
## [1] 2
# subtract
10 - 3
## [1] 7
# multiply
10 * 3
## [1] 30
# Division
10/3
## [1] 3.333333
#Exponent
10^3
## [1] 1000
# Natural log
log(10)
## [1] 2.302585
# Squareroot
sqrt(10)
## [1] 3.162278
x <- 10
y <- 20
x+y
## [1] 30
x-y
## [1] -10
x*y
## [1] 200
x/y
## [1] 0.5
x^y
## [1] 1e+20
log(x)
## [1] 2.302585
sqrt(x)
## [1] 3.162278
##----- Operations with Vectors
y <- c(2, 5, 6, 9, 10)
length(y) # count the elements
## [1] 5
sum(y) # sum values of the vector
## [1] 32
mean(y) # Average
## [1] 6.4
sum(y) / length(y)
## [1] 6.4
log(y) # Natural log of y
## [1] 0.6931472 1.6094379 1.7917595 2.1972246 2.3025851
sqrt(y) # square root value
## [1] 1.414214 2.236068 2.449490 3.000000 3.162278
2 * y # Scalar multiplication
## [1] 4 10 12 18 20
y^2
## [1] 4 25 36 81 100
### -- Get specific position of the vecor
y[1]
## [1] 2
y[3]
## [1] 6
y[ c(2,3)]
## [1] 5 6
y[ 1:3 ]
## [1] 2 5 6