Vector's differences across data frames

Hi there!
I have 4 players, that are plying a game that have 6 "ideal" strategies. They can play variations of each strategy, so the plays does not match perfectly with how the strategy is, but i would like to add a column for the closest strategy that they are playing..

strategies<- data.frame(
name=c("strategy 1","strategy 2", "strategy 3","strategy 4","strategy 5","strategy 6"),
rebel=c(1,0,0,2,0,1),
cyber=c(0,1,3,0,0,2),
chrono=c(1,0,1,0,1,0),
void=c(0,0,0,1,0,0))

plays<- data.frame(
player=c("player 1","player 2","player 3","player 4"), rebel=c(1,0,3,NA),
cyber=c(1,NA,2,2),
chrono=c(1,1,0,3),
void=c(0,1,0,0))

What ineed to do is to grab player 1, compare him with each strategy and choose the strategy that is closer to the "ideal" one from "strategies" data frame. So i was thinking about

  1. calculating the difference between "player 1" play against each strategy (i would have one vector for each posible strategy)
  2. sum each number's absolute of that each vector
  3. Choose the strategy that has the min value, that means that is closer than the others.

For "player 1", it would be "strategy 1, since

  1. "player 1" -"strategy 1"= c(0,1,0,0).
  2. 0+1+0+0=1
  3. Strategy 1 has the min value, so its the closest strategy for "player 1"

There is any function that allows me to label every player's strategy with vectors?
I am not really good with vectors and I can not figure it out how to solve this without doing it manual.

I am not sure this is what you want. The function MyFunc returns as modified form of plays with a new column listing the number of the nearest strategy. The NA values in plays are ignored and in case of a tie, the lowest valued strategy is chosen.

strategies<- data.frame(
  name=c("strategy 1","strategy 2", "strategy 3","strategy 4","strategy 5","strategy 6"),
  rebel=c(1,0,0,2,0,1),
  cyber=c(0,1,3,0,0,2),
  chrono=c(1,0,1,0,1,0),
  void=c(0,0,0,1,0,0))

plays<- data.frame(
  player=c("player 1","player 2","player 3","player 4"), rebel=c(1,0,3,NA),
  cyber=c(1,NA,2,2),
  chrono=c(1,1,0,3),
  void=c(0,1,0,0))

MyFunc <- function(S,P) {
  Strats <- vector("numeric", length = nrow(P))
  for (i in 1:nrow(P)){
    x <- P[i, -1]
    tmp <- apply(X = S[,-1],MARGIN =1,FUN = `-`, x)
    tmp <- lapply(tmp, abs)
    tmp <- sapply(tmp, sum, na.rm = TRUE)
    Strats[i] <- which.min(tmp)
  }
  P$Strategy <- Strats
  P
}

playsNew <- MyFunc(strategies, plays)
playsNew
#>     player rebel cyber chrono void Strategy
#> 1 player 1     1     1      1    0        1
#> 2 player 2     0    NA      1    1        3
#> 3 player 3     3     2      0    0        6
#> 4 player 4    NA     2      3    0        3

Created on 2020-06-29 by the reprex package (v0.3.0)

1 Like

Thank you, thats an amazing answer.

I could make it work to my example! I will try to understand the things i do not know

Thank you for your time!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.