How to multiply entire row with a matching row name in another dataframe?

You can do

test * test2[rownames(test), "mean"]
#     X  Y
# T1  1  4
# T2  4 10
# T3 15 30

You need to convert rownames to column first and then join and do any aggregation you need, i.e.

library(tidyverse)

test %>% 
 rownames_to_column('id') %>% 
 left_join(test2 %>% rownames_to_column('id'), by = 'id') %>% 
 mutate_at(vars(c('X', 'Y')), list(~ . * mean)) %>% 
 select(-mean)

#  id  X  Y
#1 T1  1  4
#2 T2  4 10
#3 T3 15 30

Just posting because I was already working on it. Almost same solution as @Sotos, but with across:

library(dplyr)
test %>% 
  rownames_to_column() %>% 
  left_join(test2 %>% rownames_to_column()) %>% 
  mutate(across(X:Y, ~.*mean)) %>% 
  select(-mean)

Joining, by = "rowname"
  rowname  X  Y
1      T1  1  4
2      T2  4 10
3      T3 15 30

Tags:

R