How can I use sum function in R? - r

This is my first post here and I couldn't find the answer I was looking for.
I'm currently taking edX course on Probability in Data Science, but I got stuck on section 1.
The task asks you to simulate a series of 6 games with random, independent outcomes of either a loss (0) or win(1), and then use the sum function to determine whether a simulated series contained at least 4 wins.
Here's what I did:
l <- list(0:1)
n <- 6
games <- expand.grid(rep(l, n))
games <- paste (games$Var1, games$Var2, games$Var3, games$Var4, games$Var5, games$Var6)
sample (game, 1, replace = TRUE)
but I can't seem to use the sum function to sum the result of '''sample''' and check if there's a series of at least 4 games. I've been trying to use
sum(sample (game, 1, replace = TRUE))
but can't seem to get anywhere with it.
Any light would be greatly appreciated!
Thanks a lot!

This is what one simulated series look like
sample(c(0, 1), 6, replace = TRUE)
To count number of wins (i.e 1) you could use sum like
sum(sample(c(0, 1), 6, replace = TRUE)) >= 4
Now you could generate such series n times with replicate.
n <- 1000
replicate(n, sum(sample(c(0, 1), 6, replace = TRUE)) >= 4)
If you have to use games to calculate you can use rowSums to count number of 1's
sum(rowSums(games) >= 4)
#[1] 22

Related

understanding the output of a function

Trying to understand how the value of "traded" is 34
available <- c(10,4,7,10,12)
desired <- c(12,5,2,6,14)
traded <- sum(mapply(function(x,y) min(x,y), available, desired))
Correct value for traded is 34. Just not sure why this is the case. I thought the value would be 6 as the minimum values from each vector (4 and 2) summed together =6
This is answered in the comments, but I wanted to add this breakdown since it helps me to visualize each step.
mapply(function(x,y) min(x,y)): Maps min(x,y) to each item in vectors x and y , so the function is doing this:
min(10,12)
min(4,5)
min(7,2)
min(10,6)
min(12,14)
and outputs = (10, 4, 2, 6, 12)
sum(mapply(...)): Which "sees" the output above and computes 10+4+2+6+12 = 34

R: expand.grid with the same vector repeated multiple times

So I'm trying to get all the possible combinations of rolling two dice n number of times.
Currently I have:
# Get all possible rolls with two dice
d <- list(1, 2, 3, 4, 5, 6)
rolls <- lapply(apply(expand.grid(d, d), 1, identity), unlist)
# Get all possible ways to roll two dice twice
trials <-lapply(apply(expand.grid(rolls, rolls), 1, identity), unlist)
d stores all possible values you can get on a single dice. rolls stores all possible outcomes of rolling two dice at the same time. And trials stores all possible outcomes of rolling two dice at the same time, twice in a row.
I can modify the last line as
trials <-lapply(apply(expand.grid(rolls, rolls, rolls), 1, identity), unlist)
to get all possible outcomes of rolling two dice at the same time, three times in a row, but I cannot figure out how to make the number of times variable, so that I could pass some arbitrary number n and get all possible outcomes of rolling two dice at the same time, an n number of times in a row
I know I only made this post about 20 minutes ago but I actually already managed to figure it out. The solution is:
trials <-lapply(apply(expand.grid(rep(list(rolls), times = n)), 1, identity), unlist)
Assuming you want permutations (both 1,2 and 2, 1) and not combinations (just 1,2), this is simpler:
n <- 2
perms <- expand.grid(as.data.frame(matrix(1:6, 6, n)))
dim(perms)
# [1] 36 2
n <- 4
perms <- expand.grid(as.data.frame(matrix(1:6, 6, n)))
dim(perms)
#[1] 1296 4
n <- 6
perms <- expand.grid(as.data.frame(matrix(1:6, 6, n)))
dim(perms)
# [1] 46656 6

how to add numbers at even position in fibonacci series in R?

The fibonacci series is obtained by adding together the prior two integers in the series, the Series include 1, 1, 2 , 3, 5 , 8. I used the following code to have series till 50.
y <- 50
}
fibvals <- numeric(y)
fibvals[1] <- 1
fibvals[2] <- 1
for (i in 3:y) {
fibvals[i] <- fibvals[i-1]+fibvals[i-2]
}
Now i want to add the numbers at even position, i.e 1, 3, 8 till 50th number? please help?
Try using seq to select the even vector indices from 2 to 50, like this:
sum(fibvals[seq(2, 50, by = 2)])
Also: there are R libraries to make working with series easier. You could use the numbers package for example, to get the first 50 Fibonacci numbers:
fibvals <- sapply(1:50, numbers::fibonacci)

Generate random numbers with 3 to 7 digits in R

How can I generate random numbers of varying length, say between 3 to 7 digits with equal probability.
At the end I would like the code to come up with a 3 to 7 digit number (with equal probability) consisting of random numbers between 0 and 9.
I came up with this solution but feel that it is overly complicated because of the obligatory generation of a data frame.
options(scipen=999)
t <- as.data.frame(c(1000,10000,100000,1000000,10000000))
round(runif(1, 0,1) * sample_n(t,1, replace = TRUE),0)
Is there a more elegant solution?
Based on the information you provided, I came up with another solution that might be closer to what you want. In the end, it consists of these steps:
randomly pick a number len from [3, 7] determining the length of the output
randomly pick len numbers from [0, 9]
concatenate those numbers
Code to do that:
(len <- runif(1, 3, 7) %/% 1)
(s <- runif(len, 0, 9) %/% 1)
cat(s, sep = "")
I previously provided this answer; it does not meet the requirements though, as became clear after OP provided further details.
Doesn't that boil down to generating a random number between 100 and 9999999?
If so, does this do what you want?
runif(5, 100, 9999999) %/% 1
You could probably also use round, but you'd always have to round down.
Output:
[1] 4531543 9411580 2195906 3510185 1129009
You could use a vectorized approach, and sample from the allowed range of exponents directly in the exponent:
pick.nums <- function(n){floor(10^(sample(3:7,n,replace = TRUE))*runif(n))}
For example,
> set.seed(123)
> pick.nums(5)
[1] 455 528105 89241 5514350 4566147

division between rows in R

I'm trying to create a new column in my matrix that is the rate of change from one point in time to the next. Using the the following matrix, this is a 3 step process.
n <- 20
data <- matrix(rnorm(2 * n), nrow = n)
1) Focusing on column 1, I want to divide row 2 by row 1.
2) I want to create a new column to hold the answer in row 2
3) repeat this process down the rows (3/2, 4/3,6/5, etc.)
I'm assuming a simple function like the following would be involved in step 1
y<-data[1,1]
z<-data[2,1]
roc<- function(x){(z/y)}
Step 2 is simple
data$ROC[data[1,] >= 0]<- roc
But I'm at a loss for step 3, and I'm not 100% sure that the function is correctly written.
Complete answer based off Ryan's comment.
####data matrix####
n <- 20
data <- matrix(rnorm(2 * n), nrow = n)
####math####
y<-data[,1]/data.table::shift(data[,1])
####new column####
data$ROC[data[1,] >= 0]<- y

Resources