[R] How create columns for squared values from previous columns?

C W
2017-04-29T00:57:03Z

Dear R list,

I am am a little unsure what is the best way to approach this. I suppose I
have

> dat <- matrix(rnorm(30), ncol = 5)
> dat <- data.frame(dat)
> dat
           X1          X2          X3         X4          X5
1 -1.13999917 -0.87868106 -0.33000492  1.5241765 -0.92483388
2 -0.56168006 -0.08837883  1.96237792 -0.5335615  0.02880586
3  0.82800071 -1.89965562 -0.05438815 -0.9162857 -0.57470053
4 -0.03218412 -0.23119263 -1.10671765 -0.2885518 -0.30953951
5  1.70525779 -0.93854817 -1.05932636 -0.2983139 -0.21980145
6  1.19047531  0.38301678 -0.20830015 -0.6668266  0.82578534

Suppose I want to add columns X6, X7, X8, where
X6 = X1^2
X7 = X2^2
X8 = X3^2

I am thinking of using apply(), but df asks for column names, what's a
quick way to generate names on the fly?

Thank you very much!

	[[alternative HTML version deleted]]