0

I have a 40x18 matrix. What I want to do is to check (for loop) every value "x" in this matrix and then if >=0, the x should be transformed to x^alpha. If x<0, then transform it in the way -lamda*(-x)^beta.

At the end, I want to have a new matrix with the transformer values.

I tried this way but it doesn't work. Can anyone help?

Here is a part of my data:

V1 = c(-0.0488238351305964, -0.0365464622704548, -0.023110113947345, -0.00936478818716672, -0.0014143836369377, 0.0136298911422536), 
V2 = c(-0.0440798156253931, -0.0290469503666371, -0.0184194158583475,-0.00659023901355601, 0.0104814403440645, 
0.02050543245721), df[1:40,18)
V3 = c(-0.0500446221600135, -0.0310561032780763, -0.0202547384070556, -0.00900829333252385, 0.0179628052483861, 0.024328936936393))

and here is part of the code I tested

for (h in 1:18) {
  if df[,h] >= 0,
    df1[,h] <- df[,h]^alpha` else df1[,h] <- df[,h]^beta`
}
DoRemy95
  • 614
  • 3
  • 19
  • Could you make the code in your question reproductible? Here it is unclear what `df` is, why there are ``` symbols in the loop, and the format of `if` can't work. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) for advice on making a reproducible example. – Alexlok Nov 30 '20 at 18:36

1 Answers1

1

You don't need a for loop, you can directly take the values of interest in mat and replace them:

V1 = c(-0.0488238351305964, -0.0365464622704548, -0.023110113947345, -0.00936478818716672, -0.0014143836369377, 0.0136298911422536)
V2 = c(-0.0440798156253931, -0.0290469503666371, -0.0184194158583475,-0.00659023901355601, 0.0104814403440645, 0.02050543245721)
V3 = c(-0.0500446221600135, -0.0310561032780763, -0.0202547384070556, -0.00900829333252385, 0.0179628052483861, 0.024328936936393)

mat <- matrix(c(V1,V2,V3), ncol = 3)
mat
#>              [,1]         [,2]         [,3]
#> [1,] -0.048823835 -0.044079816 -0.050044622
#> [2,] -0.036546462 -0.029046950 -0.031056103
#> [3,] -0.023110114 -0.018419416 -0.020254738
#> [4,] -0.009364788 -0.006590239 -0.009008293
#> [5,] -0.001414384  0.010481440  0.017962805
#> [6,]  0.013629891  0.020505432  0.024328937

alpha <- 2
lambda <- 0.1
beta <- 0.9

mat[mat >= 0] <- (mat[mat>=0])^alpha
mat[mat < 0]  <- -lambda * (-mat[mat < 0])^beta
mat
#>               [,1]          [,2]          [,3]
#> [1,] -0.0066034167 -0.0060230406 -0.0067518327
#> [2,] -0.0050881606 -0.0041380000 -0.0043947307
#> [3,] -0.0033683862 -0.0027463018 -0.0029913965
#> [4,] -0.0014939916 -0.0010889578 -0.0014427074
#> [5,] -0.0002725904  0.0001098606  0.0003226624
#> [6,]  0.0001857739  0.0004204728  0.0005918972

Created on 2020-11-30 by the reprex package (v0.3.0)

This operation is slightly more readable on example data:

mat2 <- matrix(1:9, nrow = 3)
mat2
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9

mat2[mat2 >= 5] <- mat2[mat2 >= 5] + 1
mat2[mat2 < 5] <- mat2[mat2 < 5] - 1
mat2
#>      [,1] [,2] [,3]
#> [1,]    0    3    8
#> [2,]    1    6    9
#> [3,]    2    7   10

Created on 2020-11-30 by the reprex package (v0.3.0)

Alexlok
  • 2,999
  • 15
  • 20
  • thank you very much for your help. According to instructions I should use for loop, but if it's laborious, then I do it this way you already provided – Nenad Knezevic Nov 30 '20 at 18:26
  • 2
    Oh if you're following instructions, then sure you will want to use loops. I'll leave that answer here which may be useful to other people reading that question. – Alexlok Nov 30 '20 at 18:35