I have a dataframe in R that looks like this:
head(pitch_type_df)
# A tibble: 6 x 20
# Groups: player_name, pitcher [3]
player_name pitcher pitch~1 relea~2 relea~3 pfx_x~4 pfx_z relea~5 relea~6 relea~7 spin_~8 spin_~9 velo_~*
<chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Abbott, Co~ 676265 FF 91.3 2253. -0.959 1.43 -2.60 5.48 6.27 0.837 219. 0
2 Abbott, Co~ 676265 KC 84.4 2291. 0.0784 -0.459 -2.48 5.85 6.01 0.220 39.9 -6.93
3 Abbott, Co~ 676265 SL 86.6 2133. -0.852 0.486 -2.47 5.88 6.06 0.390 225. -4.85
4 Abreu, Alb~ 656061 SI 98.5 2200. -1.22 1.10 -1.71 5.69 6.62 0.874 220. 0
5 Abreu, Alb~ 656061 SL 88.6 2265. 0.209 0.457 -2.05 5.74 6.43 0.145 190. -9.76
I would like to implement some way to use expand_grid to add two columns, plate_x and plate_z, that are sequences from -1.75 to 1.75 and 0 to 4.5, respectively. How can I add this to my dataframe in a way that a grid is created from the sequenced values to get a dataframe with all unique combinations of plate_x and plate_z for each row, without it putting together a grid with unique combinations of all the other columns? The grid to be combined with the original for each row would look like this:
expand_grid(plate_x = seq(-1.75, 1.75, by = .1), plate_z = seq(0, 4.25, by = .1))
# A tibble: 1,548 x 2
plate_x plate_z
<dbl> <dbl>
1 -1.75 0
2 -1.75 0.1
3 -1.75 0.2
4 -1.75 0.3
5 -1.75 0.4
6 -1.75 0.5
7 -1.75 0.6
8 -1.75 0.7
9 -1.75 0.8
10 -1.75 0.9
# ... with 1,538 more rows
# i Use `print(n = ...)` to see more rows
My end product would be each row from the first dataframe added to this grid, for each row.
CodePudding user response:
We may use crossing
library(tidyr)
crossing(pitch_type_df,
expand_grid(plate_x = seq(-1.75, 1.75, by = .1),
plate_z = seq(0, 4.25, by = .1)))