# First, every thing you type in a line after the # is a comment .
# So, this is a comment ; we can type ANYTHING here and it has nothing to do with the R code !
#
# Second, everything after the double ## is a result of the R code .
#
# Third, R is case sensitive; that is, a and A mean two different things !
#
#
# Now, use R as a calculator :
#
#
# Use + for addition.
5+(-6)
## [1] -1
#
# For now, do not worry about the [1] after the ##
#
#
# Use - for subtraction.
4-9
## [1] -5
4-(-9)
## [1] 13
#
#
# Use * for multiplication.
4*9
## [1] 36
4*(-9)
## [1] -36
#
#
# Use ** for power.
(-4)**2 # -4 to the power 2.
## [1] 16
4**9 # 4 to the power 9.
## [1] 262144
#
#
# Use the R function sqrt(x) for square root of the number x .
sqrt(4) # squre root of 4.
## [1] 2
sqrt(9) # square root of 9.
## [1] 3
sqrt(111) # square root of 111.
## [1] 10.53565
#
# Recall, the square root of a negative number is a complex number;
# the square root of negative one is the number i; i stands for imaginary.
# The function sqrt(x) does not return a complex number when you ask for the square root of a negative number x.
# Instead, it produces a NaN error. for example:
#
sqrt(-1) # square root of negative one.
## Warning in sqrt(-1): NaNs produced
## [1] NaN
#
## Warning in sqrt(-1): NaNs produced
## [1] NaN
#
# NaN means : Not a Number, in general; in this case, it means : Not a real Number.
#
# So, to get the complex square root, we need to cast the negative number as a complex number
# using as.complex before applying the sqrt() function:
sqrt(as.complex(-1))
## [1] 0+1i
## [1] 0+1i
# That is, the square root of negative one is 0 + 1i; or i .
#
sqrt(as.complex(-100))
## [1] 0+10i
## [1] 0+10i
# The square root of negative 100 is 10i.
#
#
# Use the R function round(x) to round the number x to a whole number
round(3.14) # round the number 3.14 to a whole number.
## [1] 3
#
round(3.54) # round the number 3.54 to a whole number.
## [1] 4
#
round(sqrt(111)) # round the square root of 111 to a whole number.
## [1] 11
#
# Use round(x,n) to round the number x to n decimal place(s).
round(3.54,1) # round the number 3.54 to ONE decimal place.
## [1] 3.5
#
round(3.643,2) # round the number 3.643 to TWO decimal places.
## [1] 3.64
#
round(3.14159, 3) # round the number 3.14159 to THREE decimal places.
## [1] 3.142
#
# in R, to get the number pi, we type pi
pi
## [1] 3.141593
#
round(pi, 2) # round the number pi to TWO decimal places.
## [1] 3.14
#
#
# Use the R function log(x,n) to find the logarithm of x to the base n .
log(23,10) # log 23 of base 10 ; that is, the common log.
## [1] 1.361728
log(23,2) # log 23 of base 2.
## [1] 4.523562
#
# Use log(x) to calculate log x of base e; that is, the natural log.
log(23) # log 23 of base e; that is, the natural log of 23.
## [1] 3.135494
# In Algebra, we use ln for the natrual log, that is, log base e .
#
#
# Now, let's calculate the number of classes of a sample of size n.
# using the formula 1 + (3.31)log(n,10) to calculate the number of classes.
#
# Give a name to the number of classes, say, c for the number of classes
# in R, we use <- as the assignment symbol
c <- 1 + (3.31)*log(23,10) # c is the number of classes of a sample of size n = 23.
# To see the value of c, we need to type in c
c
## [1] 5.507319
#
round(c) # round the number of classes c to a whole number.
## [1] 6
#
#
# Use / for division
4/9
## [1] 0.4444444
round((4/9),1) # round the number 4/9 to ONE decimal place.
## [1] 0.4
round((4/9),4) # round the number 4/9 to FOUR decimal places.
## [1] 0.4444
#
# Recall: divided by Zero is undefined .
4/0
## [1] Inf
# in R: 4/0 is Inf; Inf means Infinity;
# in Calculus, 4/0 is Infinity.....But, if you don't know calculus,
# do NOT worry about Inf.
#
#
# Now, do the Project 1 as a Team.
Use R as a calculator to evaluate each of the following expressions:
Round all of your answers to TWO decimal places for full credit!!!
1) \((-6)+(-5)\)
2) \((-11)-(-22)\)
3) \(\frac{-6}{25}\div\) \(\frac{\pi}{13}\)
4) \((-6)\times\)\((-11)\)
5) \((-7)^{2}\times\)\((3.05)^{3}\)
6) \(\sqrt{100}\div\)\(\sqrt{\pi}\)
7) \(\sqrt{-36}\)
8) \(\log_{10}(100)\)
9) \(1+3.31\times \log_{10}(30)\)
10) \(\log_{2}(10)\)
11) \(\ln{(10)}\)
(By The Way, I used R Markdown to write this document.)