简体   繁体   English

如何使用 R 删除 dataframe 中的第一行和最后 N 行?

[英]How to remove first and last N rows in a dataframe using R?

I have a dataframe df of 2M+ rows that looks like this:我有一个 dataframe df的 2M+ 行,看起来像这样:

        LOCUS POS COUNT
1: CP007539.1   1     4
2: CP007539.1   2     7
3: CP007539.1   3    10
4: CP007539.1   4    15
5: CP007539.1   5    21
6: CP007539.1   6    28

Currently I am using this in order to remove the first and last 1000 rows:目前我正在使用它来删除第一行和最后 1000 行:

> df_adj = df[head(df$POS,-1000),]
> df_adj = tail[(df_adj$POS,-1000),]

But even I can see that this can be done in a better way.但即使是我也能看出这可以用更好的方式来完成。 Suggestions?建议?

You can perform this specifying the range of rows you want to leave in the final dataset: 您可以执行此操作,以指定要在最终数据集中保留的行范围:

 df_adj <- df[1001:( nrow(df) - 1000 ),]

Just make sure you have enough rows to perform this. 只要确保您有足够的行来执行此操作即可。 A safer approach might be: 一种更安全的方法可能是:

df_adj <- if( nrow(df) > 2000 ) df[1001:( nrow(df) - 1000 ),] else df

Another way would be to combine seq_len with nrow and range :另一种方法是将seq_lennrowrange结合起来:

df <- head(iris, 10)      
df[-range(seq_len(nrow(df))), ]

which will generate the output below:这将生成下面的 output:

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa
# 7          4.6         3.4          1.4         0.3  setosa
# 8          5.0         3.4          1.5         0.2  setosa
# 9          4.4         2.9          1.4         0.2  setosa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM