Extract rows where value appears in any of multiple columns

4

Let' say I have two data.frames

name_df = read.table(text = "player_name
a
b
c
d
e
f
g", header = T)

game_df = read.table(text = "game_id winner_name loser_name
1 a b
2 b a
3 a c
4 a d
5 b c
6 c d
7 d e
8 e f
9 f a
10 g f
11 g a
12 f e
13 a d", header = T)

name_df contains a unique list of all the winner_name or loser_name values in game_df. I want to create a new data.frame that has, for each person in the name_df a row if a given name (e.g. a) appears in either the winner_name or loser_name column

So I essentially want to merge game_df with name_df, but the key column (name) can appear in either winner_name or loser_name.

So, for just a and b the final output would look something like:

final_df = read.table(text = "player_name game_id winner_name loser_name
a 1 a b
a 2 b a
a 3 a c
a 4 a d
a 9 f a
a 11 g a
a 13 a d
b 1 a b
b 2 b a
b 5 b c", header = T)
Share
Improve this question
5
  • Your description says that it should appear in either winner_name or loser_name. then why are the otther elements not selected – akrun Apr 24 at 22:41
  • Based on your description, the logic would be game_df %>% filter(winner_name %in% name_df$player_name| loser_name %in% name_df$player_name) – akrun Apr 24 at 22:42
  • Or game_df %>% filter(if_any(c(winner_name, loser_name), ~ . %in% name_df$player_name)) – akrun Apr 24 at 22:43
  • That is also not giving the expected i.e. map_dfr(setNames(name_df$player_name, name_df$player_name), ~ game_df %>% filter(winner_name %in% .x|loser_name %in% .), .id = 'player_name') – akrun Apr 24 at 22:46
  • Please check your input. The game_df 13th row shows 'a' for 'winner_name' but it is not included in final_df – akrun Apr 24 at 22:48

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue