har*_*mug 7 arrays indexing r vector
这可能是多余的,但我在SO上找不到类似的问题.
是否有一个快捷方式来获取向量或数组中的最后n个元素/条目而不使用计算中向量的长度?
foo <- 1:23
> foo
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Run Code Online (Sandbox Code Playgroud)
假设有人想要最后7个实体,我想避免这种繁琐的语法:
> foo[(length(foo)-6):length(foo)]
[1] 17 18 19 20 21 22 23
Run Code Online (Sandbox Code Playgroud)
Python有foo[-7:].R中有类似的东西吗?谢谢!
Das*_*son 13
你想要这个tail功能
foo <- 1:23
tail(foo, 5)
#[1] 19 20 21 22 23
tail(foo, 7)
#[1] 17 18 19 20 21 22 23
x <- 1:3
# If you ask for more than is currently in the vector it just
# returns the vector itself.
tail(x, 5)
#[1] 1 2 3
Run Code Online (Sandbox Code Playgroud)
除了矢量的最后/前n个元素之外,head还有很简单的方法可以获取所有内容.
x <- 1:10
# Grab everything except the first element
tail(x, -1)
#[1] 2 3 4 5 6 7 8 9 10
# Grab everything except the last element
head(x, -1)
#[1] 1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5955 次 |
| 最近记录: |