0
> w
                 A           B
1998-01-08 -0.0051653999  0.007
1998-01-09  0.0064191599 -0.008
1998-01-12 -0.0018169993  0.009
1998-01-13  0.0046482541 -0.005
1998-01-14  0.0080997329 -0.006
1998-01-15 -0.0007764179  0.008

c<-(-0.5,-0.6,-0.7,0.1,0.2,0.3)

我想创建一个矩阵,其中一列从 w 中选择 A 或 B,条件是 c > 0

IE

w[,if (c<0) A else B]
                 C           
1998-01-08 -0.0051653999
1998-01-09  0.0064191599
1998-01-12 -0.0018169993
1998-01-13 -0.005
1998-01-14 -0.006
1998-01-15  0.008
4

2 回答 2

1

您错过c了连接向量的函数(-0.5,-0.6,-0.7,0.1,0.2,0.3),我用作ind该向量的名称。然后可以使用ifelse如下函数完成索引:

> ind <-c(-0.5,-0.6,-0.7,0.1,0.2,0.3)
> data.frame(C=ifelse(ind<0, w$A, w$B), row.names=rownames(w))
                      C
1998-01-08 -0.005165400
1998-01-09  0.006419160
1998-01-12 -0.001816999
1998-01-13 -0.005000000
1998-01-14 -0.006000000
1998-01-15  0.008000000
于 2013-05-24T13:45:17.953 回答
0

试试看这个Extract matrix column values by matrix column name 总之,我想你想试试这个命令: if(c<0) w[,"A"] else w[,"B"]

于 2013-05-24T13:37:59.663 回答