假设我有一组列,在我的示例中名为Intercept, 1, 2,3以及一组系数,名为c0through 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)
Run Code Online (Sandbox Code Playgroud)
我想将 X 的每一列乘以相应的系数。
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
Run Code Online (Sandbox Code Playgroud)
这给出了我想要的。
但是,如果我的数据为 tibble ( tidyX <- as_tibble(X)),那么执行此操作的简洁方法是什么?
tidyX %>% ... ?
Run Code Online (Sandbox Code Playgroud)
这看起来很简单,我想它dplyr::rowwise()可能涉及,但我没有看到这样做的惯用方法。