Fernanda
Fernanda

Reputation: 313

Data transformation in R- columns

My dataframe with n dates

Date         team_home  team_away   prob_home   draw    prob_away
01/01/2021   Brazil     Germany      95.0        5.0     0.0
01/01/2021   England    Belgium      50.0        10.0    40.0
02/01/2021   Belgium    Canada       90.0        7.0     3.0
02/01/2021   Germany    France       60.0        10.0    30.0
 ...          ....       ...         ...         ...     ...

DESIRED DATAFRAME. Important: Only one date per row

Date          prob_Brazil    draw_Brazil_Germany  prob_Germany  prob_England  draw_England_Belgium  prob_Belgium ....
01/01/2021    95.0           5.0                    0.0            50.0          10.0                40.0
02/01/2021    NA             NA                    60.0            NA              NA               90.0      

Thank you for your help!

Upvotes: 0

Views: 52

Answers (1)

Anoushiravan R
Anoushiravan R

Reputation: 21938

You can use this but the output may not be quite desirable:

library(tidyr)

df %>%
  pivot_wider(names_from = c(team_home, team_away), 
              values_from = c(prob_home, draw, prob_away))

Upvotes: 2

Related Questions