Multiply every row of dataframe (or datatable...) with with matrix

I can't get to make this work. I would like to multiply every row (case) of the data.frame (or datatable....) "ri_data" with matrix "gew", that I become 6 new variables, which I want to compare group dependent.

``` r
#make data
group<-c("A", "B", "C")
a<-c(1, 2, 3)
b<-c(2, 3, 4)
c<-c(3, 4, 5)
d<-c(4, 5, 6)
e<-c(5, 6, 7)
f<-c(6, 7, 8)
ri_data <- data.frame(a, b, c, d, e, f, group)

gew <- matrix(data=1:36, nrow=6, ncol=6)

Created on 2022-02-03 by the reprex package (v2.0.1)

Thanks for help!

maybe this

(res_mult <- ri_data %>% 
  group_by(group) %>% 
  group_map(~as.matrix(.) %*% gew %>% data.frame))

(res_fin <- res_mult %>%
  bind_rows() %>% 
  bind_cols(ri_data$group) %>%

Thank you, that's exactly what I was looking for :slight_smile:

This is a more compact alternative:

res <- cbind(as.matrix(ri_data[1:6]) %*% gew, ri_data["group"])
colnames(res) <- names(ri_data)

Wow, thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.