rdplyrtidyversepoly

dplyr: Using poly function to generate polynomial coefficients


I want to append polynomial coefficient to data.frame as the example given below:

df1 <- 
  structure(list(
    Y = c(4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 16, 16, 16, 
          16, 16, 32, 32, 32, 32, 32, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 16, 
          16, 16, 16, 16, 32, 32, 32, 32, 32, 4, 4, 4, 4, 4, 8, 8, 8, 8, 
          8, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32)), 
    class = "data.frame", row.names = c(NA, -60L))

library(tidyverse)
df1 %>%
  dplyr::mutate(
    Linear    = poly(x = Y, degree = 3, raw = TRUE)[ ,1]
  , Quadratic = poly(x = Y, degree = 3, raw = TRUE)[ ,2]  
  , Cubic     = poly(x = Y, degree = 3, raw = TRUE)[ ,3]
    )

I wonder if there is a concise method like this

df1 %>%
  dplyr::mutate(poly(x = Y, degree = 3, raw = TRUE))

Thanks


Solution

  • Not exactly the way you were hoping, but close enough. I first convert the output of poly (a matrix) to a data.frame, then use !!! to splice the columns (turning each element of a list/data.frame into it's own argument). setNames is optional for renaming the columns:

    library(dplyr)
    
    df1 %>%
      mutate(!!!as.data.frame(poly(x = .$Y, degree = 3, raw = TRUE))) %>%
      setNames(c("Y", "Linear", "Quadratic", "Cubic"))
    

    Result:

        Y Linear Quadratic Cubic
    1   4      4        16    64
    2   4      4        16    64
    3   4      4        16    64
    4   4      4        16    64
    5   4      4        16    64
    6   8      8        64   512
    7   8      8        64   512
    8   8      8        64   512
    9   8      8        64   512
    10  8      8        64   512
    11 16     16       256  4096
    12 16     16       256  4096
    13 16     16       256  4096
    14 16     16       256  4096
    15 16     16       256  4096
    16 32     32      1024 32768
    17 32     32      1024 32768
    18 32     32      1024 32768
    19 32     32      1024 32768
    20 32     32      1024 32768
    ...