rlapplypoly

R: bind columns after lapply() the poly() function


I want to add columns containing polynomials to a dataframe (DF).

Background: I need to use polynomials in a glmnet setting. I cannot call poly() directly in the glmnet() estimation command. I get an error, likely because my “Xtrain” data contain factors. My workaround is to slice my Xtrain DF in two pieces, one containing all factors (for which no transformation is needed) and one containing the rest, viz. the numeric columns.

Now I want to add columns with polynomials to my numeric DF. Here is a minimal example of my problem.

# Some data
x <- 1:10
y <- 11:20
df = as.data.frame(cbind(x,y))

# Looks like this
    x  y
1   1 11
2   2 12
3   3 13

# Now I generate polys
lapply(df, function(i) poly(i, 2, raw=T)[,1:2])

However, I cannot figure out how to "cbind" the results. What I want to have in the end is a DF in which x, x^2, y, y^2, are contained. Order does not matter. However, ideally I would also have column labels (to identify the polys). For instance like this:

     x x2 y  y2
 1   1 1 11 121
 2   2 4 12 144
 3   3 9 13 169

Thank you... Cheers!


Solution

  • Another option is

    as.data.frame(lapply(df, function(i) poly(i, 2, raw=T)[,1:2]))
    #   x.1 x.2 y.1 y.2
    #1    1   1  11 121
    #2    2   4  12 144
    #3    3   9  13 169
    # ...
    

    As mentioned by @gpier and @akrun already, you might use ^ instead of poly

    n <- 2
    df[paste(names(df), n, sep = "_")] <- df^n
    df