Predict the output of each when executed in R.
x = choose(4,2) x = x + 5 x
x = choose(5,3) x = 3 * x x
x = sqrt(100) x = x / 4 x
x = factorial(4) x = x + 5 x
a) 11 b) 30 c) 2.5 d) 29
Predict the output of each of the following when executed in R:
nums = 2:4 + seq(from=3, by=2, length.out=6) nums[c(TRUE,FALSE)]
nums = seq(from=2,by=3,length.out=6) + c(2,3,5) nums[-(2:4)]
(a) 5 11 14 (b) 4 17 22
Predict the output of each when executed in R.
> nums = c(5, 8, 3, 2, 7, 4) + c(1, 2, 3) > nums[c(2:4)]
> nums = c(5, 8, 3, 2, 7, 4) - c(3, 0, 1) > nums[-c(2:4)]
> nums = c(3, 1, 5, 4) + c(2) > nums[c(1,3)]
> nums = c(9,7,6,2,8) > nums[length(nums)]
a) 10 6 3 b) 2 7 3 c) 5 7 d) 8
Without using the c()
function, what single R command will produce the given sequence?
4 16 36 64 100 144 196 256 324 400
8 64 216 512 1000 1728 2744 4096 5832 8000
4 4 4 9 9 9 16 16 16
4 9 16 4 9 16 4 9 16
a) (2*(1:10))^2 b) (2*(1:10))^3 c) rep((2:4)^2,each=3) d) rep((2:4)^2,times=3)
Create the following vectors:
$(4,6,3,4,6,3,\ldots,4,6,3,4)$ where there are $11$ occurrences of $4$
$(4,4,\ldots,4,6,6,\ldots,6,3,3,\ldots,3)$ where there are $10$ occurrences of $4$, $20$ occurrences of $6$, and $30$ occurrences of $3$.
rep()
function.
Create the following vector in R and then find the sum of its elements:
$$(0.1^3 0.2^1, 0.1^6 0.2^4, 0.1^9 0.2^7, \ldots, 0.1^{36} 0.2^{34})$$$0.0002000016$
Use R to find the following sum for any given positive integer $n$: $${}_nC_0 + {}_{n}C_1 + {}_{n}C_2 + \cdots + {}_nC_n$$ Find this sum for several values of $n$. Do you notice a pattern?
When $n=5$, the sum should be $32$.
Suppose $v$ has been defined in R with
> v = c(2,5,3,6,8,9,1,10)What command in R will return the vector consisting of every other element (starting with the first)?
Answers vary, but the following all work:
v[c(TRUE,FALSE)]
v[seq(from=1,to=length(v),by=2)]
v[(1:(length(v)/2))*2-1]
Suppose $v$ has been defined in R with
> v = c(2,5,3,6,8,9,1,10)What command in R will return a vector with elements identical to those in v, but in reverse order?
v[length(v):1]
What command in R will produce the vector with the following elements in the following order (with a minimum effort)?
1 11 21 2 12 22 3 13 23 4 14 24 ... 9 19 29
rep(1:9,each=3) + rep(((0:2)*10),times=9)
n = 1:100 sum(log((n+1)/n))Of course, the even quicker way is to use the properties of logs to collapse this telescoping sum to
log(101)Recall for both expressions above that in R,
log(x)
evaluates to $\ln x$.
In the well-known "Birthday Problem", one asks what is the probability that 2 or more people in a room of $n$ people share a common birthday. This can be shown to be
$$1 - \frac{{}_{365} P_n}{365^n}$$What R command will produce these probabilities for $1 \le n \le 25$?
What is the smallest number of people in the room where the probability that 2 or more people share a common birthday exceeds 50%?
n = 1:25 1-factorial(n)*choose(365,n)/365^nNoting that the 23rd element of the vector produced by the above is the first to exceed 0.50, it takes minimally 23 people in the room for the chances of two people sharing a birthday to be better than half.
What command in R will produce the first 15 terms of the following sequence?
$$\{2^3 \cdot 3, 3^3 \cdot 5, 4^3 \cdot 7, 5^3 \cdot 9, \cdots\}$$n = 1:15 (n+1)^3*(2*n+1)
Given R vector $x = (x_1, x_2, \ldots, x_n)$,
Construct in R the vector $(x_1 + 2x_2 - x_3, x_2 + 2x_3 - x_4, \ldots, x_{n-2} + 2x_{n-1} - x_n)$.
Calculate the value of $\displaystyle{\sum_{i=1}^{n-1} \frac{e^{-x_{i+1}}}{x_i+10}}$.
x = c(8,3,2,9,1)
, then the results should be:
# (a) 12 -2 19 # (b) 0.03254871
The function cumprod(v)
produces a vector of cumulative products of the elements of a numerical vector $v$. For example,
> cumprod(c(2,3,4,7)) [1] 2 6 24 168Use this function to calculate to $6$ decimal places the value of the expression $$1 + \frac{2}{3} + \left( \frac{2}{3} \cdot \frac{4}{5} \right) + \left( \frac{2}{3} \cdot \frac{4}{5} \cdot \frac{6}{7} \right) + \cdots + \left( \frac{2}{3} \cdot \frac{4}{5} \cdots \frac{38}{39} \right)$$
6.976346