R
comes with many functions that you can use to do sophisticated tasks like random sampling. For example, you can round a number with the round()
function, or calculate its factorial with the factorial()
function. Using a function is pretty simple. Just write the name of the function and then the data you want the function to operate on in parentheses.
Find round(3.1415)
, factorial(12)
, mean(1:17)
.
You can create an object named die
that contains the numbers one through six:
# Create a vector with numbers from 1 to 6
die <- 1:6
You can simulate a roll of the die with R
’s sample()
function. sample()
takes two arguments: a vector named x
and a number named size
.
Try sample(x = die, size = 1)
. Is there any difference if you type sample(die, size = 1)
?
What does args(round)
returns? Round 3.14159265 to 4 decimal places.
round(3.1415)
## [1] 3
factorial(12)
## [1] 479001600
mean(1:17)
## [1] 9
-There is no difference in the expected output (numbers one through six). Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods.
sample(x = die, size = 1)
## [1] 3
sample(die, size = 1)
## [1] 6
sample(die, 1)
## [1] 5
Displays the argument names and corresponding default values of a function or primitive.
args(round)
## function (x, digits = 0)
## NULL
round(3.14159265, digits = 4)
## [1] 3.1416
Use R
as a calculator to compute the following values:
27*(38-17)
## [1] 567
log(14*7)
## [1] 4.584967
sqrt(436/12)
## [1] 6.027714
Create the following vectors in R
:
\(a = (5, 10, 15, 20, ..., 160)\)
\(b = (87, 86, 85, ..., 56)\)
Multiply these vectors and call the result d
. Select subsets of d
to identify the following.
What are the 19th, 20th, and 21st elements of d
?
What are all of the elements of d
which are less than 2000?
# Creating vectotr 'a' by 5
a <- seq(5, 160, by = 5)
# Creating vectotr 'b' by -1
b <- seq(87, 56, -1)
# Multipying vector 'a' and vector 'b'
d <- a*b
# Vector 'd', element at position '19'
d[19]
## [1] 6555
# Vector 'd', element at position '20'
d[20]
## [1] 6800
# Vector 'd', element at position '21'
d[21]
## [1] 7035
# Vector 'd', elements less than '2000'
d[d < 2000]
## [1] 435 860 1275 1680
What result do you get if you type mean(1:1337)
? Is it different if you type mean(c(1,1337))
? Why?
What R command can you use to select five different letters at random?
sample(letters, 5, replace=T)
sample(letters, 5, replace=F)
sample(1:26, 5, replace=T)
c(5,letters)
sample(LETTERS, 5, replace=F)
Same outcome : 669
Averaging Any Long Series of Consecutive Numbers
\[ Average = \frac{X_1+X_n}{2}\]
1:1337
generate a vector 1,2,3…1337 and then calculate the mean:
mean(1:1337)
## [1] 669
c(1,1337)
generate a two values vector [1, 1337] the first and last element of vector 1:1337
mean(c(1,1337))
## [1] 669
# If replace equal TRUE, the output could have duplicate values
sample(letters, 5, replace=T)
## [1] "h" "f" "a" "w" "z"
# If replace equal FALSE, the output wont have duplicate values
sample(letters, 5, replace=F)
## [1] "p" "l" "r" "f" "a"
sample(1:26, 5, replace=F)
## [1] 13 11 26 4 20
c(5,letters)
## [1] "5" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"
## [18] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
sample(LETTERS, 5, replace=F)
## [1] "E" "O" "P" "A" "W"
We should use solution b
or e
(lowercase or uppercase) for selecting different letters, replace = F
wont let us have an output with same letters.
Look up the usage of the command seq
(you can use ?seq
or help(seq)
to obtain help from within R
) Now determine what the following commands achieve:
x <- seq(from = 0, to = 4*pi, length.out=1000)
y <- sin(x)
plot(x, y, type = "l", main = "graph of sin(x)")
What happens if you leave out type="l"
of the plot command?
Removing type="l"
changes the graph type and uses p
points as default value
Possible types:
x <- seq(from = 0, to = 4*pi, length.out=1000)
y <- sin(x)
plot(x, y, main = "graph of sin(x)")