3

I've seen the answers about multiplying one matrix by another. But I have a single matrix, completely numerical. As an example:

matrix = read.table(text = 
"ID  Mult    t1  t2  t3  t4  t5
 4   0.164   10  20  30  40  50
 16  0.581   5   10  5   10  5
 42  0.008   16  17  18  19  20
 91  0.328   20  20  20  20  20
 103 0.108   103 42  56  84  61", 
 h = T)

I want to multiply t1 through t5 by the multiplier, separately, and put the results in new columns in my matrix.

I would do it column by column, but for the fact that I have over 200 columns!

Hopefully, someone can suggest a more simple fix.

Uwe
  • 41,420
  • 11
  • 90
  • 134
PWillrodt
  • 31
  • 1
  • 1
    Welcome to SO. It's always a good idea to list other sources you've consulted, and explaining how your situation is different (otherwise your question may get flagged as a repeat). Have you looked at [this post](http://stackoverflow.com/q/12673891/1152809)? Also, see [this post](http://stackoverflow.com/q/5963269/1152809) on how to ask an R question that easily allows others to help you. It specifies using `dput` to output your data so that we can easily run it on our local R and see what you see. Also, don't forget to upvote anything helpful, and mark an answer as correct. – Travis Heeter Jan 17 '17 at 17:32

2 Answers2

1

You could do:

df=read.table(text="ID  Mult    t1  t2  t3  t4  t5
4   0.164   10  20  30  40  50
16  0.581   5   10  5   10  5
42  0.008   16  17  18  19  20
91  0.328   20  20  20  20  20
103 0.108   103 42  56  84  61",h=T)

df[,c(paste0(colnames(df[,grepl("^t.*",colnames(df),perl = T)]),"bis"))]=df[,grepl("^t.*",colnames(df),perl = T)]*df$Mult

df[,grepl("^t.*",colnames(df),perl = T)] subsets df to only have the columns starting with "t"
df[,c(paste0(colnames(df[,grepl("^t.*",colnames(df),perl = T)]),"bis"))] take the colnames() of the previous subset, and concatenate them using paste0() with "bis" or any string to indicate the change. This creates the new columns which are filled with the results of the multiplication.

> df
   ID  Mult  t1 t2 t3 t4 t5  t1bis t2bis t3bis t4bis t5bis
1   4 0.164  10 20 30 40 50  1.640 3.280 4.920 6.560 8.200
2  16 0.581   5 10  5 10  5  2.905 5.810 2.905 5.810 2.905
3  42 0.008  16 17 18 19 20  0.128 0.136 0.144 0.152 0.160
4  91 0.328  20 20 20 20 20  6.560 6.560 6.560 6.560 6.560
5 103 0.108 103 42 56 84 61 11.124 4.536 6.048 9.072 6.588
Haboryme
  • 4,611
  • 2
  • 18
  • 21
0

Some don't like loops, but I do sometimes. This is one of those times :)

mydf<-matrix
for (i in 3:length(mydf)){
  mydf[,length(mydf)+1] <- mydf$Mult * mydf[i]
}
mydf
   ID  Mult  t1 t2 t3 t4 t5   t1.1  t2.1  t3.1  t4.1  t5.1
1   4 0.164  10 20 30 40 50  1.640 3.280 4.920 6.560 8.200
2  16 0.581   5 10  5 10  5  2.905 5.810 2.905 5.810 2.905
3  42 0.008  16 17 18 19 20  0.128 0.136 0.144 0.152 0.160
4  91 0.328  20 20 20 20 20  6.560 6.560 6.560 6.560 6.560
5 103 0.108 103 42 56 84 61 11.124 4.536 6.048 9.072 6.588
akaDrHouse
  • 2,190
  • 2
  • 20
  • 29