一些R命令(5)

> x=array(runif(24),c(4,3,2))
> x
, , 1

          [,1]      [,2]       [,3]
[1,] 0.6953832 0.8209977 0.38583214
[2,] 0.4993301 0.5251950 0.04634707
[3,] 0.8219069 0.1761397 0.66012447
[4,] 0.1118249 0.7220923 0.46044622

, , 2

          [,1]      [,2]      [,3]
[1,] 0.8021535 0.6029162 0.6863334
[2,] 0.5999509 0.4068553 0.8726527
[3,] 0.9140723 0.4912114 0.6791098
[4,] 0.5755712 0.1285849 0.4201371

> is.matrix(x)
[1] FALSE
> dim(x)
[1] 4 3 2
> is.matrix(x[1,,])
[1] TRUE
> x=array(1:24,c(4,3,2))
> x
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

> apply(x,1,mean)
[1] 11 12 13 14
> apply(x,1,sum)
[1] 66 72 78 84
> apply(x,1:2,sum)
     [,1] [,2] [,3]
[1,]   14   22   30
[2,]   16   24   32
[3,]   18   26   34
[4,]   20   28   36
> apply(x,c(1,3),sum)
     [,1] [,2]
[1,]   15   51
[2,]   18   54
[3,]   21   57
[4,]   24   60
> x=matrix(1:20,5,4)
> x
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> sweep(x,1,1:5,"*")
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    4   14   24   34
[3,]    9   24   39   54
[4,]   16   36   56   76
[5,]   25   50   75  100
> sweep(x,x,1:4,"+")
Error in if (lstats > prod(dimmargin)) { : 
  missing value where TRUE/FALSE needed
> sweep(x,2,1:4,"+")
     [,1] [,2] [,3] [,4]
[1,]    2    8   14   20
[2,]    3    9   15   21
[3,]    4   10   16   22
[4,]    5   11   17   23
[5,]    6   12   18   24
> x*1:5
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    4   14   24   34
[3,]    9   24   39   54
[4,]   16   36   56   76
[5,]   25   50   75  100
> x
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> #下面把x标准化,即每一元素减去该列均值,除以该列标准差
> (x=matrix(sample(1:100,24),6,4))
     [,1] [,2] [,3] [,4]
[1,]   15   95   13   83
[2,]   86   50   32   75
[3,]    7   25    4   92
[4,]   71   26   93    1
[5,]   18   34   20   35
[6,]   61   47   87   73
> x1=scale(x)
> x1
           [,1]        [,2]       [,3]       [,4]
[1,] -0.8320666  1.87189305 -0.7361859  0.6658389
[2,]  1.2778166  0.14694041 -0.2453953  0.4359090
[3,] -1.0697999 -0.81136661 -0.9686657  0.9245102
[4,]  0.8320666 -0.77303433  1.3303009 -1.6909435
[5,] -0.7429166 -0.46637609 -0.5553683 -0.7137410
[6,]  0.5349000  0.03194357  1.1753144  0.3784265
attr(,"scaled:center")
[1] 43.00000 46.16667 41.50000 59.83333
attr(,"scaled:scale")
[1] 33.65115 26.08767 38.71305 34.79320
> x2=scale(x,scale=F)
> x2
     [,1]        [,2]  [,3]      [,4]
[1,]  -28  48.8333333 -28.5  23.16667
[2,]   43   3.8333333  -9.5  15.16667
[3,]  -36 -21.1666667 -37.5  32.16667
[4,]   28 -20.1666667  51.5 -58.83333
[5,]  -25 -12.1666667 -21.5 -24.83333
[6,]   18   0.8333333  45.5  13.16667
attr(,"scaled:center")
[1] 43.00000 46.16667 41.50000 59.83333
> apply(x,1,mean)
[1] 51.50 60.75 32.00 47.75 26.75 67.00
> apply(x,3,mean)
Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed
> apply(x,2,mean)
[1] 43.00000 46.16667 41.50000 59.83333
> x3=scale(x,center=F)
> x3
          [,1]      [,2]       [,3]       [,4]
[1,] 0.2591141 1.6694437 0.21771544 1.11850087
[2,] 1.4855876 0.8786546 0.53591492 1.01069356
[3,] 0.1209199 0.4393273 0.06698937 1.23978410
[4,] 1.2264735 0.4569004 1.55750274 0.01347591
[5,] 0.3109369 0.5974851 0.33494683 0.47165699
[6,] 1.0537308 0.8259353 1.45701869 0.98374173
attr(,"scaled:scale")
[1] 57.88955 56.90518 59.71097 74.20647
> round(apply(x1,2,mean),14)
[1] 0 0 0 0
> apply(x1,2,sd)
[1] 1 1 1 1
> apply(x,2,sd)
[1] 33.65115 26.08767 38.71305 34.79320
> 28/33
[1] 0.8484848
> 28/33.65115
[1] 0.8320667
> x2
     [,1]        [,2]  [,3]      [,4]
[1,]  -28  48.8333333 -28.5  23.16667
[2,]   43   3.8333333  -9.5  15.16667
[3,]  -36 -21.1666667 -37.5  32.16667
[4,]   28 -20.1666667  51.5 -58.83333
[5,]  -25 -12.1666667 -21.5 -24.83333
[6,]   18   0.8333333  45.5  13.16667
attr(,"scaled:center")
[1] 43.00000 46.16667 41.50000 59.83333
> x3
          [,1]      [,2]       [,3]       [,4]
[1,] 0.2591141 1.6694437 0.21771544 1.11850087
[2,] 1.4855876 0.8786546 0.53591492 1.01069356
[3,] 0.1209199 0.4393273 0.06698937 1.23978410
[4,] 1.2264735 0.4569004 1.55750274 0.01347591
[5,] 0.3109369 0.5974851 0.33494683 0.47165699
[6,] 1.0537308 0.8259353 1.45701869 0.98374173
attr(,"scaled:scale")
[1] 57.88955 56.90518 59.71097 74.20647
> round(apply(x2,2,mean),14)
[1] 0 0 0 0
> round(apply(x2,2,sd),14)
[1] 33.65115 26.08767 38.71305 34.79320
> round(apply(x3,2,sd),14)
[1] 0.5812992 0.4584411 0.6483406 0.4688702
> 33.65115*0.2591141
[1] 8.719487
> round(apply(x3,2,mean),14)
[1] 0.7427938 0.8112910 0.6950147 0.8063089
> 43+28
[1] 71
> -x
     [,1] [,2] [,3] [,4]
[1,]  -15  -95  -13  -83
[2,]  -86  -50  -32  -75
[3,]   -7  -25   -4  -92
[4,]  -71  -26  -93   -1
[5,]  -18  -34  -20  -35
[6,]  -61  -47  -87  -73
> x
     [,1] [,2] [,3] [,4]
[1,]   15   95   13   83
[2,]   86   50   32   75
[3,]    7   25    4   92
[4,]   71   26   93    1
[5,]   18   34   20   35
[6,]   61   47   87   73
> 86-7
[1] 79
> 15/79
[1] 0.1898734
> x3
          [,1]      [,2]       [,3]       [,4]
[1,] 0.2591141 1.6694437 0.21771544 1.11850087
[2,] 1.4855876 0.8786546 0.53591492 1.01069356
[3,] 0.1209199 0.4393273 0.06698937 1.23978410
[4,] 1.2264735 0.4569004 1.55750274 0.01347591
[5,] 0.3109369 0.5974851 0.33494683 0.47165699
[6,] 1.0537308 0.8259353 1.45701869 0.98374173
attr(,"scaled:scale")
[1] 57.88955 56.90518 59.71097 74.20647
> x
     [,1] [,2] [,3] [,4]
[1,]   15   95   13   83
[2,]   86   50   32   75
[3,]    7   25    4   92
[4,]   71   26   93    1
[5,]   18   34   20   35
[6,]   61   47   87   73
> 15/33.65115
[1] 0.44575
> round(apply(x,2,sd),14)
[1] 33.65115 26.08767 38.71305 34.79320
> x
     [,1] [,2] [,3] [,4]
[1,]   15   95   13   83
[2,]   86   50   32   75
[3,]    7   25    4   92
[4,]   71   26   93    1
[5,]   18   34   20   35
[6,]   61   47   87   73
> 15/33.65115
[1] 0.44575
> x3=scale(x,center=F)
> x3
          [,1]      [,2]       [,3]       [,4]
[1,] 0.2591141 1.6694437 0.21771544 1.11850087
[2,] 1.4855876 0.8786546 0.53591492 1.01069356
[3,] 0.1209199 0.4393273 0.06698937 1.23978410
[4,] 1.2264735 0.4569004 1.55750274 0.01347591
[5,] 0.3109369 0.5974851 0.33494683 0.47165699
[6,] 1.0537308 0.8259353 1.45701869 0.98374173
attr(,"scaled:scale")
[1] 57.88955 56.90518 59.71097 74.20647
> 0.2591141*57.88955
[1] 15
> 1.4855876*57.88955
[1] 86


上一篇:   一些R命令(6)

下一篇:   一些R命令(4)



评论

登录之后就可以评论 / 回复啦(#^.^#)    点此登录    点此注册

评论列表

暂无评论!快写一条吧(๑′ᴗ‵๑)