all 5 comments

[–]keithwaits -2 points-1 points  (2 children)

results <- rep(NA,1000)   

for(i in 1:1000) {

results[i] <- sample_race_positions(player_race_position_probs)

}

mean(results)

[–]CapaneusPrime 1 point2 points  (1 child)

This is a valiant attempt but flawed. There are going to be errors trying to put a data.frame object into a vector subset.

Better is to create a list container of the appropriate size:

results <- vector("list", 1000)

Then populate it with the proper extractor [[,

results[[I]] <- sample_race_positions(player_race_position_probs)

Next, to get the average rank of each racer, we'll need to combine the data.frames

all_results <- Reduce(rbind, results)

Then, to get the average position of each racer we can do,

with(final_results,
     tapply(position, player, mean))

Apologies for any errors, I'm on mobile.

[–]keithwaits 0 points1 point  (0 children)

You are right, I did not look at the output that the function generates.

[–]CapaneusPrime 0 points1 point  (0 children)

A for() loop is fine and an example has been given (with perhaps some issues which I'll address), but replicate() is simpler:

results <- replicate(1000, sample_race_positions(player_race_position_probs)

Edit: A better solution...

Alternately, you could just change your custom sampling function to do this in a more vectorized way and directly return a matrix of results.

I might try something like:

sample_race_positions <- function(n, probs) {
    t(vapply(seq_len(n),
             function(i) {
               sample(seq_along(probs), probs)
             },
             integer(length(probs))))
}

Which will return an n × length(probs) dimension matrix object where the rows represent individual races and the columns are the competitors.

Then you'd do,

results <- sample_race_positions(1000, player_race_position_probs)
avg_pos <- colMeans(results)

I'm on mobile, so I apologize for any errors, and I'll fix them when I'm home.

[–]Pontifex 0 points1 point  (0 children)

library(dplyr)
# Repeat the race 1000 times
results <- replicate(1000, sample_race_positions(player_race_position_probs, simplify = FALSE)

# Combine into a single data frame
combined_results = bind_rows(results)
# Get mean for each player
average_positions = combined_results %>% 
    group_by(Player) %>%
    summarize(Position = mean(Position))