Quick way to run R-squared analysis in R

I want to calculate the R-Squared of each of the stats in columns 3-5 (targets, receptions, and yards) and how it correlates to following-year fpts. Below is a sample of the type of data I'm working with:

player year targets receptions yards fpts
Michael Gallup 2017 113 66 1107 147
D.J. Moore 2017 135 87 1175 144
Robert Woods 2017 139 90 1134 143
Calvin Ridley 2017 93 63 866 134
Terry McLaurin 2017 93 58 919 134
Tyler Boyd 2017 148 90 1046 133
Deebo Samuel 2017 81 57 802 132
Michael Gallup 2018 91 62 779 132
D.J. Moore 2018 89 58 860 130
Robert Woods 2018 127 83 997 130
Calvin Ridley 2018 100 58 900 129
Terry McLaurin 2018 133 74 1035 127
Tyler Boyd 2018 97 66 869 124
Deebo Samuel 2018 84 48 740 122
Michael Gallup 2019 122 78 833 120
D.J. Moore 2019 106 67 778 118
Robert Woods 2019 105 54 627 118
Calvin Ridley 2019 90 49 1001 112
Terry McLaurin 2019 96 52 779 110
Tyler Boyd 2019 90 47 775 110
Deebo Samuel 2019 92 59 680 104

I'd like to get it so it appears like this, with the "next_year_fpts" column

player year targets receptions yards fpts next_year_fpts
Michael Gallup 2017 113 66 1107 147 132
D.J. Moore 2017 135 87 1175 144 130
Robert Woods 2017 139 90 1134 143 130
Calvin Ridley 2017 93 63 866 134 129
Terry McLaurin 2017 93 58 919 134 127
Tyler Boyd 2017 148 90 1046 133 124
Deebo Samuel 2017 81 57 802 132 122
Michael Gallup 2018 91 62 779 132 120
D.J. Moore 2018 89 58 860 130 118
Robert Woods 2018 127 83 997 130 118
Calvin Ridley 2018 100 58 900 129 112
Terry McLaurin 2018 133 74 1035 127 110
Tyler Boyd 2018 97 66 869 124 110
Deebo Samuel 2018 84 48 740 122 104
Michael Gallup 2019 122 78 833 120 NA
D.J. Moore 2019 106 67 778 118 NA
Robert Woods 2019 105 54 627 118 NA
Calvin Ridley 2019 90 49 1001 112 NA
Terry McLaurin 2019 96 52 779 110 NA
Tyler Boyd 2019 90 47 775 110 NA
Deebo Samuel 2019 92 59 680 104 NA

This way I'll be able to calculate R-Squared for the targets, receptions, etc. in year x compared to the fpts in year x+1. I'll have several years of data with many more players, so looking for quick and reproducible way to make my data the way that I need it.

You could copy your dataframe, replace year by year - 1 and then merge the columns back in...

dat2 <- dat
dat2$year <- dat2$year-1
dat2$next_year_fpts <- dat2$fpts
dat <- merge(dat, dat2[, c("player", "year", "next_year_fpts")])

not a tidyverse solution, but it should work, and there would be an analogous solution with tidyverse I'm sure.

Tidyverse solution:

df = tibble::tibble(
  player = c('a','b', 'a','b', 'a','c'),
  year   = c(2017, 2017, 2018, 2018, 2019,2019),
  targets= c(113,114,115,116,115,100),
  fpts   = c(147,148,132,144,130,12)
)
df
#> # A tibble: 6 x 4
#>   player  year targets  fpts
#>   <chr>  <dbl>   <dbl> <dbl>
#> 1 a       2017     113   147
#> 2 b       2017     114   148
#> 3 a       2018     115   132
#> 4 b       2018     116   144
#> 5 a       2019     115   130
#> 6 c       2019     100    12

tidyr::pivot_wider(df,id_cols=player,names_from=year,values_from=c(fpts,targets))
#> # A tibble: 3 x 7
#>   player fpts_2017 fpts_2018 fpts_2019 targets_2017 targets_2018 targets_2019
#>   <chr>      <dbl>     <dbl>     <dbl>        <dbl>        <dbl>        <dbl>
#> 1 a            147       132       130          113          115          115
#> 2 b            148       144        NA          114          116           NA
#> 3 c             NA        NA        12           NA           NA          100

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

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