如何在 R 中选择向量的顶部或底部 n 个元素?


可以使用 R 中的 head 和 tail 函数来选择顶部或底部元素。当我们希望了解矢量中的数据或对部分数据执行某些计算时需要此项功能。

示例

考虑下面的向量,我们将使用 head 和 tail 通过使用正负号来选择这些向量中的顶部和底部元素。这会有不同的方式来选择元素。

> x<-letters[1:26]
> x
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> head(x,3)
[1] "a" "b" "c"
> head(x,5)
[1] "a" "b" "c" "d" "e"
> head(x,10)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> tail(x,3)
[1] "x" "y" "z"
> tail(x,5)
[1] "v" "w" "x" "y" "z"
> tail(x,10)
[1] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> tail(x,-3)
[1] "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
[20] "w" "x" "y" "z"
> tail(x,-5)
[1] "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x"
[20] "y" "z"
> tail(x,-10)
[1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> head(x,-3)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w"
> head(x,-5)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u"
> head(x,-10)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"
> y<-1:50
> y
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> head(y,5)
[1] 1 2 3 4 5
> head(y,10)
[1] 1 2 3 4 5 6 7 8 9 10
> tail(y,5)
[1] 46 47 48 49 50
> tail(y,10)
[1] 41 42 43 44 45 46 47 48 49 50
> head(y,-5)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
> head(y,-10)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
> tail(y,-5)
[1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
[26] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> tail(y,-10)
[1] 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
[26] 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> set.seed(9)
> z<-sample(letters[1:20],30,replace=TRUE)
> z
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n" "s" "c" "n" "e" "i" "f" "k" "c" "k" "q"
> head(z,3)
[1] "f" "s" "c"
> head(z,5)
[1] "f" "s" "c" "l" "p"
> head(z,10)
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j"
> tail(z,3)
[1] "c" "k" "q"
> tail(z,5)
[1] "f" "k" "c" "k" "q"
> tail(z,10)
[1] "s" "c" "n" "e" "i" "f" "k" "c" "k" "q"
> head(z,-3)
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n" "s" "c" "n" "e" "i" "f" "k"
> head(z,-5)
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n" "s" "c" "n" "e" "i"
> head(z,-10)
[1] "f" "s" "c" "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b"
[20] "n"
> tail(z,-3)
[1] "l" "p" "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b" "n" "s" "c"
[20] "n" "e" "i" "f" "k" "c" "k" "q"
> tail(z,-5)
[1] "l" "e" "r" "e" "j" "l" "c" "p" "i" "a" "f" "b" "o" "b" "n" "s" "c" "n" "e"
[20] "i" "f" "k" "c" "k" "q"
> tail(z,-10)
[1] "l" "c" "p" "i" "a" "f" "b" "o" "b" "n" "s" "c" "n" "e" "i" "f" "k" "c" "k"
[20] "q"

更新于: 10-8-2020

519 次浏览

开启你的 职业

完成课程以获得认证

开始
广告