Change row values to zero if less than row standard deviation

前端 未结 2 459
小鲜肉
小鲜肉 2021-01-19 04:58

I want to change all values of a row to zero if they are less than the standard deviation of that row.

set.seed(007)
X <- data.frame(matrix(sample(c(5:50)         


        
2条回答
  •  -上瘾入骨i
    2021-01-19 05:45

    How about this?

    X[t(apply(X, 1, function(x) x - sd(x) < 0))] <- 0
    #    X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
    # 1  50  0 34 36 41 31  0 18 45  20
    # 2  23 15 18 17 22 38 28 32 45   0
    # 3   0 40 50  0 39 40 40 43 16  46
    # 4   0  0 46  0 25 33 36 33 39   0
    # 5  16 25 50 22 46 38 30  0 22  38
    # 6  41  0  0 43 19 22 35 31  0  31
    # 7  20 30 33 27  0 12 26 25  0  29
    # 8  49  0 27 41 42  0 27 25 40  21
    # 9   0 50 49 43 46 22 20 33 21  42
    # 10 26 19 21 26 49 17 24 47 24  13
    

提交回复
热议问题