A.6 Add/Edit columns with mutate
Let’s add a column to our data frame for home score differential (home score minus away score). We can add columns to our data frame, or edit existing columns, in base R like this.
date gid away home ascore hscore diff
1 2021-10-19 22100001 BKN MIL 104 127 23
2 2021-10-19 22100002 GSW LAL 121 114 -7
3 2021-10-20 22100011 OKC UTA 86 107 21
4 2021-10-20 22100013 SAC POR 124 121 -3
5 2021-10-20 22100012 DEN PHX 110 98 -12
6 2021-10-20 22100010 ORL SAS 97 123 26
This can also be done by using mutate
.
date gid away home ascore hscore diff
1 2021-10-19 22100001 BKN MIL 104 127 23
2 2021-10-19 22100002 GSW LAL 121 114 -7
3 2021-10-20 22100011 OKC UTA 86 107 21
4 2021-10-20 22100013 SAC POR 124 121 -3
5 2021-10-20 22100012 DEN PHX 110 98 -12
6 2021-10-20 22100010 ORL SAS 97 123 26