0

How can I create squared columns? I know how to do this for individual columns, but I need a loop/function that takes the square of multiple columns at once (all of them starter with "player"). In reality, my dataframe contains a lot of players, so i need an efficient command. Mutate_at is useful, but i also need to create new column names.

df <- data.frame(season = c(1,2,3), player1 = c(1,2,3),  player2= c(3,4,5))

#desired result: 

# season player1 player2 squared1 squared2
# 1        1        3       1        9
# 2        2        4       4        16
# 3        3        5       9        25
scarlett rouge
  • 319
  • 1
  • 7
  • See solution provided by [Matifou](https://stackoverflow.com/a/51275619/680068) and adapt as required. – zx8754 Oct 25 '21 at 09:21

0 Answers0