[R] (no subject)
Dimitri Liakhovitski
dimitri.liakhovitski at gmail.com
Wed Dec 23 22:35:17 CET 2015
Merry upcoming Christmas - for those of us who celebrate it!
# I have data frame x.
x <- data.frame(game = c(rep(1, 4), rep(2, 4)), set = rep(c(1,1,2,2),
2), player = rep(1:2, 4),
char1 = c(1,0,0,0,0,0,0,1), char2 =
c(0,0,1,0,0,1,0,0), char3 = c(0,1,0,1,0,0,0,0),
char4 = c(0,0,0,0,1,0,1,0))
x
# There are several games (2 here). Each game has several sets (2
here. In each set participate
# several players (2 here).
# Each player possesses 1 or 4 possible characteristics.
# For example, in game 1, set 1, player 1 has characteristic 1 and player 2 -
# characteristic 3
# I also have data frame myinfo:
(myinfo <- data.frame(game = 1:2, set1 = c(3, 4), set2 = c(2, 1)))
# It tells me:
# in game 1, set 1 the winner was the player with characteristic 3
# in game 1, set 2 the winner was the player with characteristic 2, etc.
# I need to merge the 2 to produce the result below.
# I just need an additional column that - for each game and each set -
# has a 1 in the row of the player who had the winning characteristic
# (as per myinfo) and has a 0 otherwise.
result <- x
result$winner <- c(0, 1, 1, 0, 1, 0, 0, 1)
result
# I have written a long loop that loops through each set of each game,
identifies
# which characteristic wins in myinfo, and puts a 1 against the winning row.
# But it's taking forever. Could it be done faster? Thanks a lot!
# Important: In my real game the number of players could be more than
2 and so can
# the number of games and the number of sets per game.
# However, we can assume that the number of sets per game is always the same,
# and the number of players per set is always the same.
--
Dimitri Liakhovitski
More information about the R-help
mailing list