Say I have a set of columns, named Intercept, 1, 2, 3 in my example, and a set of coefficients, named c0 through c3.
xs<-seq(.1,1,.1)
X <- cbind(Intercept=1, "1"=xs, "2"=xs^2, "3"=xs^3)
coefs <- c(c0=10, c1=2, c2=.5, c3=-1)
I want to multiply each column of X by the the corresponding coefficient.
sweep(
X, # x: the data array
2, # MARGIN: 2, to sweep across rows
coefs, # STATS: just the array of coefficients
`*`) # FUN: the function to use is multiplication
This gives what I want.
But if I had my data as a tibble (tidyX <- as_tibble(X)), what is the tidy way of doing this?
tidyX %>% ... ?
It seems simple, and I imagine it involves dplyr::rowwise(), perhaps, but I don't see the idiomatic way of doing this.