Home > Blockchain >  Is there a way to assign a variable input in R?
Is there a way to assign a variable input in R?

Time:09-22

I'm wondering how to replace the "x" in the code each time the for loop completes an iteration with the newly created "p_" variable. The goal is to update the data frame and remove the sampled name each time the loop completes one iteration so as not to pick the same name more than once. Is there a way to dynamically input a variable into that line?

New to R so bare with me ! Thanks!

setwd('/Users/Mike/Desktop/Random Coding Projects/Draft Simulator')

# Load data of players available to draft
df <- read.csv('Players.csv')

# Load in data for snake draft positions
snake_pos <- read.csv('snake positions.csv')

# Ask the user for draft info input
# num_drafters <- readline(prompt='Enter the number of people participating in the draft: ')
# num_drafters <- as.integer(num_drafters)
# num_rds <- readline(prompt='Enter the number of rounds in the draft: ')
# num_rds <- as.integer(num_rds)
# pick_pos <- readline(prompt = 'Enter your draft position: ')
# pick_pos <- as.integer(pick_pos)

# Explicitly Defining Draft Parameters
num_drafters <- 10
num_rds <- 15
pick_pos <- 2

# Assign Picks to Teams
t1_picks <- snake_pos[1,]
pick_nos <- snake_pos[pick_pos,]
t3_picks <- snake_pos[3,]
t4_picks <- snake_pos[4,]
t5_picks <- snake_pos[5,]
t6_picks <- snake_pos[6,]
t7_picks <- snake_pos[7,]
t8_picks <- snake_pos[8,]
t9_picks <- snake_pos[9,]
t10_picks <- snake_pos[10,]

# Defining team class
base_team <- list(pl1 = '', pl2 = '', pl3 = '', pl4 = '', pl5 = '', pl6 = '',
                  pl7 = '', pl8 = '', pl9 = '', pl10 = '', pl11 = '', pl12 = '',
                  pl13 = '', pl14 = '', pl15 = '')
class(base_team) <- 'team_template'

# Creating Individual Pick Variables
total_picks <- num_drafters * num_rds
pick_list <- seq(1,total_picks,by=1)

for(i in pick_list) {
  assign(paste0('p_',i),sample(df[1:5,3], 1, replace = FALSE, prob = NULL))
  df <- df[!(df$PLAYER.NAME==x),]
}

CodePudding user response:

There isn't any df object to work with but I don't see why something along these lines wouldn't work:

set.seed(123) # for reproducibility
temp_players <- sample(df$PLAYER.NAME) # will be permuted vesion of that column
for(i in pick_list) {
  assign(paste0('p_',i),sample(df[1:5,3], 1, replace = FALSE, prob = NULL))
  df <- df[!(temp_players[i]==df$PLAYER.NAME), ]
  # I'm assuming you would want to so something with the reduced dataset.
  # Because if you don't it will get overwritten in next loop iteration
   }

Probably (almost certainly in fact) would be better to make a copy ofdf, say temp_df, and then work with that. You really shouldn't be destructively making changes in your original dataset.

CodePudding user response:

It is not entirely clear to me what you want. Using an Answer block here to ease readability. Re:

# Creating Individual Pick Variables
total_picks <- num_drafters * num_rds
pick_list <- seq(1,total_picks,by=1)

for(i in pick_list) {
  assign(paste0('p_',i),sample(df[1:5,3], 1, replace = FALSE, prob = NULL))
  df <- df[!(df$PLAYER.NAME==x),]
}

The sample(df[1:5,3], 1, replace = FALSE, prob = NULL)) will reset for each iteration of the for loop. So replace = FALSE does nothing since you are only selecting 1 item.

If you want a pick_list number of players randomly selected w/o replacement from df then:

total_picks <- num_drafters * num_rds
pick_list <- seq(1,total_picks,by=1)
randplayers <- sample(df[1:5,3], pick_list, replace = FALSE, prob = NULL))

Will give you a vector of the random players selected without replacement. Apologize if I'm off track on this. BTW, also note that because R is vectorized, for loops can usually be avoided.

  •  Tags:  
  • r
  • Related