[R] Column-by-column division

Steven Yen @tyen @end|ng |rom ntu@edu@tw
Wed Mar 3 12:00:08 CET 2021


I have a 10 x 2 matrix x. Like to divide the first column by s[1] and 
second column by s[2]. The following lines work but are clumsy. Any 
idea? Thanks.

 > x
       [,1] [,2]
  [1,]    1   11
  [2,]    2   12
  [3,]    3   13
  [4,]    4   14
  [5,]    5   15
  [6,]    6   16
  [7,]    7   17
  [8,]    8   18
  [9,]    9   19
[10,]   10   20
 > s
[1] 1 2
 > t(t(x)/s)
       [,1] [,2]
  [1,]    1  5.5
  [2,]    2  6.0
  [3,]    3  6.5
  [4,]    4  7.0
  [5,]    5  7.5
  [6,]    6  8.0
  [7,]    7  8.5
  [8,]    8  9.0
  [9,]    9  9.5
[10,]   10 10.0



More information about the R-help mailing list