R language study notes (6) - "R language primer" (operator, index, data editor, matrix calculation)

Operator:
%% modulus;
%/%divisible;
!=not equal;
==equal;
!=Non;

  1. Expression 0<x<1
    calculation process are: x = 0.5, for example, first determine the x> 0, the result is TRUE; TRUE then <1 is determined, then converted to a logical variable numerical variable, i.e., determines a <1, the result is FALSE.
    The expression R is not legitimate.
    We need to be written x>0 && x<1.
  2. Use ?Syntaxcan see the symbol-related issues
  3. Compare the same problem
    may be used ==or identicala function or all.equalfunctions
    difference, see the example below:
> x <- 1:4
> y <- 1:3
> x ==y #使用==,会判断到每一个数据,以较长的变量为准
[1]  TRUE  TRUE  TRUE FALSE
Warning message:
In x == y : longer object length is not a multiple of shorter object length
> identical(x , y) #比较数据的内在关系,如果严格相等就返回TRUE,否则返回FALSE
[1] FALSE
> all.equal(x , y)#用来判断两个变量是否近似相等,结果返回TRUE或是对二者差异的描述
[1] "Numeric: lengths (4, 3) differ"
> 0.9 == (1 - 0.1)
[1] TRUE
> identical(0.9 , 1-0.1)
[1] TRUE
> all.equal(0.9 , 1-0.1)
[1] TRUE
> 0.9 == (1.1-0.2)
[1] FALSE
> identical(0.9 , 1.1-0.2)
[1] FALSE
> all.equal(0.9 , 1.1-0.2)
[1] TRUE
> all.equal(0.9 , 1.1-0.2 , tolerance = 1e-16)
[1] "Mean relative difference: 1.233581e-16"
>

About index:
access index must use square brackets and parentheses are used to specify the function arguments.
And when lower default will dimensionality reduction, if either dimension reduction, you need to set the options dropin the following example:

> x <- matrix(1:6 , 2 , 3)
> x
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
> x[1,2]
[1] 3
> x[,3]  
[1] 5 6  #注意到结果已是向量(一维)
> x[,3, drop = FALSE]   #对drop选项进行设置,结果不降维
     [,1]
[1,]    5
[2,]    6
> x[ , 3] <- c(10 , 11)
> x
     [,1] [,2] [,3]
[1,]    1    3   10
[2,]    2    4   11
> x[-c(1,3),] #-号表示不输出,这里表示不输出第1行,且不存在第3行
[1]  2  4 11
> x <- matrix(1:9 , 3 , 3)
> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> x[-c(1,3),] #不输出第1行和第3行,默认选项drop,所以结果降维了
[1] 2 5 8
> x[-c(1,3) , drop = FALSE]
[1] 2 4 5 6 7 8 9 #不输出元素1 3
> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> x[-c(1,3) , ,drop = FALSE] #设置drop选项结果不降维
     [,1] [,2] [,3]
[1,]    2    5    8
> x <- 1:10
> x
 [1]  1  2  3  4  5  6  7  8  9 10
> x[x>=5]
[1]  5  6  7  8  9 10
> x >= 5
 [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
 [9]  TRUE  TRUE
> x[x >= 5] <- 20
> x
 [1]  1  2  3  4 20 20 20 20 20 20
> x[x %% 2 == 0] #选出偶数,方法一
[1]  2  4 20 20 20 20 20 20
> #选出偶数,方法二
> x <- 1:40;
> s <- c(FALSE , TRUE); #s的长度小于x,所以会被循环
> x[s]
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34
[18] 36 38 40

Access list elements using []? [[]]

> x <- list(1:3)
> x
[[1]]
[1] 1 2 3
> x[1] #单一的方括号返回一个列表
[[1]]
[1] 1 2 3
> x[[1]][1] #双重方括号提取列表中的对象
[1] 1

Access list name:
names , character label object element, there are multiple types of names, such as names, colnames, rownames, dimnames.

> x <- matrix(1:4,2,2)
> x
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> rownames(x) <- c("a" , "b")
> colnames(x) <- c("c" ,  "d")
> x
  c d
a 1 3
b 2 4
> dimnames(x)
[[1]]
[1] "a" "b"

[[2]]
[1] "c" "d"

> x["a",]  #提取元素,即取子集
c d 
1 3 

> a <- array(data = 1:8 , dim = c(2 , 2 , 2))#对于数组,道理类似
> a
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

> dimnames(a) <- list(c("a" , "b") , c("c" , "d") , c("e" , "f"))
> a
, , e

  c d
a 1 3
b 2 4

, , f

  c d
a 5 7
b 6 8
#称作,取子集,这是因为原始对象的属性仍然会被保留
> a["b" , "d" , "e"]
[1] 4

Data Editor:
use the data.entry()command to open a data editor, as shown below:
Here Insert Picture Description
may be the data editor, modify the data, similarly, you can also view the data de function, but the function will be de modified direct output to the screen, but data.entry () will not.
Function c()listed in parentheses for the connection object.
Using conventional arithmetic operations between vectors. Different lengths, will be shorter that the vector recycled.
Here Insert Picture Description
Here Insert Picture Description
Matrix calculation:

> m1 <- matrix(1 , nr = 2 , nc = 2)
> m1
     [,1] [,2]
[1,]    1    1
[2,]    1    1
> m2 <- matrix(2 , nr = 2 , nc =  2)
> m2
     [,1] [,2]
[1,]    2    2
[2,]    2    2
#使用cbind,rbind合并矩阵
> rbind(m1 , m2) #row_bind上下合并
     [,1] [,2]
[1,]    1    1
[2,]    1    1
[3,]    2    2
[4,]    2    2
> cbind(m1 , m2) #column_bind左右合并矩阵
     [,1] [,2] [,3] [,4]
[1,]    1    1    2    2
[2,]    1    1    2    2
> m1 %*% m2 #运算符%*%做出矩阵的乘法
     [,1] [,2]
[1,]    4    4
[2,]    4    4
> m2 %*% m1
     [,1] [,2]
[1,]    4    4
[2,]    4    4
> #转置函数t()
> #diag()函数可以提取或是修正一个矩阵的对角元,或者是创建一个对焦矩阵
> diag(m1)
[1] 1 1
> diag(m1) <- 10
> m1
     [,1] [,2]
[1,]   10    1
[2,]    1   10
> diag(2) #生成二解对角矩阵
     [,1] [,2]
[1,]    1    0
[2,]    0    1
> v <-  c(10 , 20  , 30)
> diag(v)
     [,1] [,2] [,3]
[1,]   10    0    0
[2,]    0   20    0
[3,]    0    0   30
> diag(2 , nr = 3 , nc = 5)
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    0    0    0    0
[2,]    0    2    0    0    0
[3,]    0    0    2    0    0
> #solve求矩阵的逆,qr分解矩阵,eigen计算特征值和特征向量,svd作奇异值分解
> solve(m1)
            [,1]        [,2]
[1,]  0.10101010 -0.01010101
[2,] -0.01010101  0.10101010
> 
Published 112 original articles · won praise 30 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43448491/article/details/104072986
Recommended