美文网首页R语言学习笔记
R语言学习笔记3-矩阵篇

R语言学习笔记3-矩阵篇

作者: RudyHe | 来源:发表于2015-12-03 19:07 被阅读412次
- matrix
    - y<-matrix(c(1,2,3,4),nrow=2,ncol=2)
    - y
        - 1 3
        - 2 4
    - y<-matrix(c(1,2,3,4),nrow=2)    # same as above
    - y[,2]    # 3 4
    - y[1,1]<-1
    - y[2,1]<-2
- matrix operation
    - y%*%y    # 7=1*1+2*3... matrix multiplication
        - 7 15
        - 10 22
    - y*y
        - 1 9
        - 4 16
    - 3*y
        - 3 9
        - 6 12
    - z=matrix(c(1,2,3,4,1,1,0,0,1,0,1,0),nrow=4)
    - z[,2:3]    # 1 1 0 0 1 0 1 0
    - z[1:2,]    # 1 2 1 1 1 0
    - z[1:2,2]    # 1 1
    - y<-matrix(c(1,2,3,4,5,6),nrow=3)
    - y
        - 1 4
        - 2 5
        - 3 6
    - y[c(1,3),]<-matrix(c(1,1,8,12),nrow=2)
    - y
        - 1 8
        - 2 5
        - 1 12
    - x<-matrix(nrow=3,ncol=3)
    - y<-matrix(c(4,5,2,3),nrow=2)
    - x[2:3,2:3]<-y
    - x
        - NA NA NA
        - NA 4 2
        - NA 5 3
    - y<-matrix(c(1,2,3,4,5,6),nrow=3)
    - y[-2,]
        - 1 4
        - 3 6
- pixmap example
    - library(pixmap)
    - mtrush1<-read.pnm("mtrush1.pgm")
    - mtrush1@grey[28,88]    # 0.796    using @ visit component in S4 type
    - mtrush1@grey[84:163,135:177]<-1    # cover a white rectangle
    - plot(mtrush1) 
- filter
    - x<-matrix(c(1,2,3,2,3,4),nrow=3)
    - x[x[,2]>=3,]    # remove rows whose col2<3
        - 2 3
        - 3 4
    - j<-x[,2]>=3
    - x[j,]    # same as above
    - m=matrix(c(1,2,3,4,5,6),nrow=3)
        - 1 4
        - 2 5
        - 3 6
    - m[m[,1]>1 & m[,2]>5,]    # 3 6 pick rows that col 1>1 and col 2>5,only row 3 fits
    - m=matrix(c(5,2,9,-1,10,11),nrow=3)
    - m
        - 5 -1
        - 2 10
        - 9 11
    - which(m>2)    # 1(5) 3(9) 5(10) 6(11)
    - row(m)    # so as col() function
        - 1 1
        - 2 2
        - 3 3
    - z=matrix(c(1,2,3,4,5,6),nrow=3)
    - apply(z,2,mean)    # parameter 2 means from column view, 1 means row view
        - 2 5
    - f<-function(x) x/c(2,8)
    - y<-apply(z,1,f)    # z is 3*2, but y is 2*3
    - y
        - 0.5 1.000 1.50
        - 0.5 0.625 0.75
    findols<-function(x) {
        findol<-function(xrow) {
            mdn<-median(xrow)
            devs<-abs(xrow-mdn)
            return(which.max(devs))    # return index of max devs
        }
        return(apply(x,1,findol))
    }
- add & remove
    - x<-c(x,20)    # append 20
    - x<-c(x[1:3],20,x[4:6])    # insert 20 into 4th place
    - x<-x[-2:-4]    # remove 2,3,4
    - one<-rep(1,4)
    - z<-matrix(c(1,2,3,4,1,1,0,0,1,0,1,0),nrow=4)
    - cbind(one,z)    # column bind
        - 1 1 1 1
        - 1 2 1 0
        - 1 3 0 1
        - 1 4 0 0
    - cbind(1,z)    # same as above, cycling pad
    - q<-cbind(c(1,2),c(3,4))
        - 1 3
        - 2 4
- miscellaneous
    - z<-matrix(1:8,nrow=4)
    - z
        - 1 5
        - 2 6
        - 3 7
        - 4 8
    - length(z)    # 8
    - class(z)    # "matrix"
    - attributes(z)
        - $dim
        - 4 2
    - dim(z)    # 4 2
    - nrow(z)    # 4
    - ncol(z)    # 2
    - r<-z[2,]    # r is a vector not a matrix, wrong way
    - attributes(r)    # NULL
    - r<-z[2,, drop=FALSE]    # right way
    - dim(r)    # 1 2
    - "["(z,3,2)    # same as z[3,2] 7
    - u<-1:3
    - v<-as.matrix(u)    # another right way
    - attributes(v)
        - $dim
        - 3 1
    - z<-matrix(c(1,2,3,4),nrow=2)
    - colnames(z)    # NULL
    - colnames(z)<-c("a","b")
    - z
        - a b
        - 1 3
        - 2 4
    - first
        - 46 30
        - 21 25
        - 50 48
    - second
        - 46 43
        - 41 35
        - 50 49
    - tests<-array(data=c(first,second),dim=c(3,2,2))    # high dimension array
    - attributes(tests)
        - $dim
        - 3 2 2
    - tests[3,2,1]    # 48, student 3 in first test 2 part's score

相关文章

网友评论

    本文标题:R语言学习笔记3-矩阵篇

    本文链接:https://www.haomeiwen.com/subject/eudshttx.html