R如何在应用条件的情况下进行简单的矢量/矩阵计算?

时间:2014-08-01 14:34:50

标签: r matrix vector

我有一个矢量,即 change_vector(其中日期是rownames)

2012-07-30  2012-08-06  2012-08-13  2012-08-20
100%    100%    150%    100%

和矩阵:

    2012-07-30  2012-08-06  2012-08-13  2012-08-20
2012-05-28  1.0 2.0 3.0 2.0
2012-06-04  1.0 2.0 3.0 2.0
2012-06-11  1.0 2.0 3.0 2.0
2012-06-18  1.0 2.0 3.0 2.0
2012-06-25  1.0 2.0 3.0 2.0
2012-07-02  1.0 2.0 3.0 2.0
2012-07-09  1.0 2.0 3.0 2.0
2012-07-16  1.0 2.0 3.0 2.0
2012-07-23  1.0 2.0 3.0 2.0
2012-07-30  1.0 2.0 3.0 2.0
2012-08-06  NA  2.0 3.0 2.0
2012-08-13  NA  NA  3.0 2.0
2012-08-20  NA  NA  NA  2.0
2012-08-27  NA  NA  NA  NA

如果我将矢量与我的矩阵相乘,我想做一个简单的计算 然后2012-08-13的所有栏目将增加150% 即到4.5 ..参见我做计算的这个例子

t(t(my_matrix) * (change_vector)) 

    2012-07-30  2012-08-06  2012-08-13  2012-08-20
2012-05-28  1.0 2.0 4.5 2.0
2012-06-04  1.0 2.0 4.5 2.0
2012-06-11  1.0 2.0 4.5 2.0
2012-06-18  1.0 2.0 4.5 2.0
2012-06-25  1.0 2.0 4.5 2.0
2012-07-02  1.0 2.0 4.5 2.0
2012-07-09  1.0 2.0 4.5 2.0
2012-07-16  1.0 2.0 4.5 2.0
2012-07-23  1.0 2.0 4.5 2.0
2012-07-30  1.0 2.0 4.5 2.0
2012-08-06  NA  2.0 4.5 2.0
2012-08-13  NA  NA  4.5 2.0
2012-08-20  NA  NA  NA  2.0
2012-08-27  NA  NA  NA  NA

然而,我想要的是限制乘法,使其仅计算相对于该日期的最后9周,例如,2012-08-13栏仅相对于提升日期9周的日期增加到4.5 即输出应为:

    2012-07-30  2012-08-06  2012-08-13  2012-08-20
2012-05-28  1.0 2.0 3.0 2.0
2012-06-04  1.0 2.0 3.0 2.0
2012-06-11  1.0 2.0 4.5 2.0
2012-06-18  1.0 2.0 4.5 2.0
2012-06-25  1.0 2.0 4.5 2.0
2012-07-02  1.0 2.0 4.5 2.0
2012-07-09  1.0 2.0 4.5 2.0
2012-07-16  1.0 2.0 4.5 2.0
2012-07-23  1.0 2.0 4.5 2.0
2012-07-30  1.0 2.0 4.5 2.0
2012-08-06  NA  2.0 4.5 2.0
2012-08-13  NA  NA  4.5 2.0
2012-08-20  NA  NA  NA  2.0
2012-08-27  NA  NA  NA  NA

请注意,在我的实例中,我的矩阵非常大,变化向量将有很多不同的值

由于

1 个答案:

答案 0 :(得分:1)

将来,如果您使用dput()之类的内容发布数据来读取数据,那将会很有帮助。使愿意提供帮助的人更容易。

change_vector <- structure(c(1, 1, 1.5, 1), 
    .Names = c("2012-07-30", "2012-08-06", "2012-08-13", "2012-08-20"))
my_matrix <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, NA, NA, NA, NA, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, NA, NA, NA, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 3, 3, 3, NA, NA, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    NA), .Dim = c(14L, 4L), .Dimnames = list(c("2012-05-28", "2012-06-04", 
    "2012-06-11", "2012-06-18", "2012-06-25", "2012-07-02", "2012-07-09", 
    "2012-07-16", "2012-07-23", "2012-07-30", "2012-08-06", "2012-08-13", 
    "2012-08-20", "2012-08-27"), c("2012-07-30", "2012-08-06", "2012-08-13", 
    "2012-08-20")))

这里的关键是定义另一个矩阵,可以用作是否应该应用调整的指标。请参阅下面的示例代码。

# calculate the difference in weeks between the matrix and vector dates
mdates <- as.Date(dimnames(my_matrix)[[1]])
vdates <- as.Date(names(change_vector))
difweeks <- outer(mdates, vdates, "-") / 7

我已经分开了接下来的几个步骤,试图说明发生了什么。

# define a matrix that is 1 when the adjustment should be made, and 0 otherwise
adjonlyif <- 1*(difweeks >= -9)

# subtract 1 from the change vector
cv2 <- change_vector - 1

# multiply the adjusted change vector by the adjustment matrix, add 1, and multiply by the original matrix
my_matrix * (1 + t(t(adjonlyif) * cv2))

但是,你也可以在一行代码中提交这个......

my_matrix * (1 + t(t(difweeks >= -9) * (change_vector - 1)))

我不确定你是否只想消除超过9周的东西(这是我编码的东西),或者如果你还需要消除未来的东西(你必须将代码修改为做)。这样的事情......而不是difweeks >= -9,你需要像difweeks >= -9 & difweeks <= 0这样的东西。