Replace values of specific columns in an array (R)

3

In the array ( ret_matrix )

      IBOV        PETR4        VALE5        ITUB4        BBDC4        PETR3    
[1,] -0.040630825 -0.027795652 -0.052643733 -0.053488685 -0.048455772 -0.061668282
[2,] -0.030463489 -0.031010237  0           -0.040229625 -0.030552275 -0.010409016
[3,]  0           -0.027012078 -0.022668170  0           -0.080732363  0.005218051
[4,] -0.057468428 -0.074922051 -0.068414670 -0.044130126 -0.069032911 -0.057468428
[5,]  0.011897277 -0.004705891  0.035489885  0           -0.006024115  0
[6,]  0.020190656  0            0.009715552  0.014771317  0.023881732  0.011714308
[7,] -0.007047191  0.004529286  0.004135085  0.017442303 -0.005917177 -0.007047191
[8,] -0.022650593 -0.029481336 -0.019445057  0           -0.011940440 -0.046076458
[9,]  0.033137223  0.035274722  0            0.060452104  0.017857617  0.046076458

I would like to replace the values 0 by 0.0000000001, only from columns 3 to 5, I tried the code below

ret_matriz[ret_matriz==0,3:5] <- 0.00001

But I received the following error

Error in ret_matrix [ret_matrix == 0,3: 5]:   (subscript) logical subscript too long

Where am I going wrong? What is the correct way to achieve the result described above?

    
asked by anonymous 10.06.2014 / 22:07

1 answer

5

In the array, when you put the condition ret_matriz==0 you are already making a subset of the entire array, rows and columns.

So what you have to do in this case is first is to filter only the columns you want, for example:

ret_matriz[,3:5][ret_matriz[,3:5]==0] <- 0.00001

    
11.06.2014 / 16:19