I have a dataframe that looks like the following, but with more columns and rows.
dog_c cat_c cheese_c hat_c
3 4 3 2
3 1 2 5
5 2 1 4
I would like to create a dataframe that looks like the following. I want to square all the values in a given column from the original dataframe and then create a column name that's the same as the original but with a 2 on the end. I would then like to attach it to the original dataframe.
dog_c cat_c cheese_c hat_c dog_c2 cat_c2 cheese_c2 hat_c2
3 4 3 2 9 16 9 4
3 1 2 5 9 1 2 5
5 2 1 4 25 4 1 16
I know I could do this in the following way for each new column.
df$dog_c2 <- (df$dog_c)^2
Does anyone have suggestions for how I can do this more efficiently?