r - Grouped multiplication of matrices with unequal dimensions -


i new r , trying determine how can following:

i have 2 matrices, each row date , each column number. second matrix longer first. want create function multiply first row (say january number) first 4 rows of second matrix (which january numbers well). so, i'm looking 4 results. want move second row of first matrix (february number) , multiply 4 february numbers second matrix. eventually, hoping code multiply first second if month , years match.

first matrix

jan 2007        143.75 feb 2007        140.93 

second matrix

2007-01-05         12.14 2007-01-12         10.15 2007-01-19         10.40 2007-01-26         11.13 2007-02-02         10.08 2007-02-09         11.10 2007-02-16         10.02 2007-02-23         10.58 

assuming both matrices, , dates on left row names, can try along these lines. here match months of row names of 2 matrices , use create vector calculation.

idx <- match(format(as.date(rownames(m2)), "%b"), sub(" .*", "", rownames(m1))) m2 * m1[idx] #                [,1] # 2007-01-05 1745.125 # 2007-01-12 1459.062 # 2007-01-19 1495.000 # 2007-01-26 1599.938 # 2007-02-02 1420.574 # 2007-02-09 1564.323 # 2007-02-16 1412.119 # 2007-02-23 1491.039 

data:

m1 <- structure(c(143.75, 140.93), .dim = c(2l, 1l), .dimnames = list(     c("jan 2007", "feb 2007"), null)) m2 <- structure(c(12.14, 10.15, 10.4, 11.13, 10.08, 11.1, 10.02, 10.58 ), .dim = c(8l, 1l), .dimnames = list(c("2007-01-05", "2007-01-12",  "2007-01-19", "2007-01-26", "2007-02-02", "2007-02-09", "2007-02-16",  "2007-02-23"), null)) 

note: haven't given information in post, whether or not doing multiple years, whether dates row names or columns, etc. if doing multiple years, please post more representative data example desired result.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -