I have vectors in R: RWidx <- which(grepl("RW", Data$Positions)) STidx <-…
- User
I have vectors in R: RW_idx <- which(grepl("RW", DataPositions)) GK_idx <- which(grepl("GK", DataPositions)) LW_idx <- which(grepl("LW", DataPositions)) LM_idx <- which(grepl("LM", DataPositions)) CB_idx <- which(grepl("CB", DataPositions)) LB_idx <- which(grepl("LB", DataPositions)) RM_idx <- which(grepl("RM", DataPositions)) RWB_idx <- which(grepl("RWB", Data$Positions)) From each of those vectors I need to select one player into list ( in order of the given list) and I need to ensure that selected player is not already inside the list.
- Assistant
You can use a loop to go through each of the vectors and then use a conditional statement to check if the player is already in the list or not. Here's how you can do it:
R# Create an empty list to store selected players selected_players <- list() # Create a list of vectors to loop through position_vectors <- list(RW_idx, ST_idx, GK_idx, CM_idx, LW_idx, CDM_idx, LM_idx, CF_idx, CB_idx, CAM_idx, LB_idx, RB_idx, RM_idx, LWB_idx, RWB_idx) # Loop through the position vectors for (pos_vector in position_vectors) { # Loop through the players in each position vector for (player_idx in pos_vector) { # If the player index is not already in the selected players list, add it if (!player_idx %in% unlist(selected_players)) { selected_players <- append(selected_players, list(player_idx)) break # Exit the inner loop after finding a unique player } } } - User
Now. Imagine that I have 100 of those lists in rows as DataFrame. Each element in row is bound to specific list of indexes ( as above). For each of those elements in rows there must be 9% probability of selecting new random index from the appropiate list and each element in row must be uniuqe.
- Assistant
To implement the random selection with a 9% probability for each element in the DataFrame rows, you can use the following approach:
- Combine all the index vectors into a list of lists.
- Define a function to randomly select a unique index with a 9% probability.
- Loop through each row of the DataFrame and apply the function to each element.
R - User
Can you do it without any libraries?
- Assistant
Certainly! You can achieve the same result without using any libraries as follows:
R - User
Now, following that same problem I would like to create part of genetic algorithm, the tournament selection and crossover for that data. The step that you wrote is used for mutation of offsprings.
So the 100 rows DataFrame is actually a list of potential parents. Now. THe function that evaluets the value of optimized function is called target(). Do not use external libriaries. Remeber that in each row players are from diffrent positions (above lists) and they must be unique within row.
- Assistant