当前位置:网站首页>13 r basic exercises

13 r basic exercises

2022-06-11 20:09:00 THE ORDER

> array(1:12,dim=c(2,3,2),dimnames=list(c("x1","x2"),c("x3","x4","x5"),c("x6","x7")))
, , x6

   x3 x4 x5
x1  1  3  5
x2  2  4  6

, , x7

   x3 x4 x5
x1  7  9 11
x2  8 10 12

> x=c("a","b","c","d","e","f")
> x[2]# Indexes 2, from 1 Start 
[1] "b"
> x[c(1,3,5)]  # Index vector 
[1] "a" "c" "e"
> ind=(1:3)*2 
> x[ind]      
[1] "b" "d" "f"
> x[-3]   # Don't 3 The location 
[1] "a" "b" "d" "e" "f"
> x[-(1:3)] # Don't 1-3
[1] "d" "e" "f"
> "b">"a"  #R You can compare the size of letters , In English order 
[1] TRUE
> x>"a" # Vectorization , Return to one logic vector 
[1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> x[x>"a"]# Logical vector true , Returns the corresponding subset of positions 
[1] "b" "c" "d" "e" "f"
> c(T,F,F)&c(T,T,F)# And true 
[1]  TRUE FALSE FALSE
> c(T,F,F)&&c(T,T,F)
[1] TRUE
> c(T,F,F)|c(T,T,F)# Or there may be a truth 
[1]  TRUE  TRUE FALSE
> !c(T,F)# Not 
[1] FALSE  TRUE
> x[!x>"b"]#! Take the 
[1] "a" "b"
> x=list(a=1:3,b=matrix(1,2,2),c=c(T,F))
> x# list 
$a
[1] 1 2 3

$b
     [,1] [,2]
[1,]    1    1
[2,]    1    1

$c
[1]  TRUE FALSE

> x[1]
$a
[1] 1 2 3

> x[c(1,3)]
$a
[1] 1 2 3

$c
[1]  TRUE FALSE

> x[c(T,F,T)]# Logical index 
$a
[1] 1 2 3

$c
[1]  TRUE FALSE

> x[c("a","b")]# Name Index 
$a
[1] 1 2 3

$b
     [,1] [,2]
[1,]    1    1
[2,]    1    1

> y=matrix(1:12,3,4)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> y[c(1,3),c(T,F,T,F)]# Matrix indexing 
     [,1] [,2]
[1,]    1    7
[2,]    3    9
> y[c(1,2),-4]# No second place 4 Column 
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
> y[1,]
[1]  1  4  7 10
> y[,2]
[1] 4 5 6
> y[1,,drop=F]#R Take elements from inside , The front and rear types are consistent , The matrix is a vector because drop=T Automatically simplifies 
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
原网站

版权声明
本文为[THE ORDER]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011757348469.html