如何删除R中数据框的第一行? [英] How to delete the first row of a dataframe in R?

查看:24
本文介绍了如何删除R中数据框的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 11 列的数据集,每列超过 1000 行.这些列被标记为 V1、V2、V11 等.我使用c"命令将名称替换为对我更有用的名称.我没有意识到第 1 行还包含每列的标签,而我的实际数据从第 2 行开始.

I have a dataset with 11 columns with over a 1000 rows each. The columns were labeled V1, V2, V11, etc.. I replaced the names with something more useful to me using the "c" command. I didn't realize that row 1 also contained labels for each column and my actual data starts on row 2.

有没有办法删除第1行并递减?

Is there a way to delete row 1 and decrement?

推荐答案

像这样保留原始文件中的标签:

Keep the labels from your original file like this:

df = read.table('data.txt', header = T)

如果您有名为 x 和 y 的列,您可以这样处理它们:

If you have columns named x and y, you can address them like this:

df$x
df$y

如果您想实际删除 data.frame 中的第一行,可以使用如下负索引:

If you'd like to actually delete the first row from a data.frame, you can use negative indices like this:

df = df[-1,]

如果您想从 data.frame 中删除一列,您可以为其分配 NULL:

If you'd like to delete a column from a data.frame, you can assign NULL to it:

df$x = NULL

以下是一些如何在 R 中创建和操作 data.frame 的简单示例:

Here are some simple examples of how to create and manipulate a data.frame in R:

# create a data.frame with 10 rows
> x = rnorm(10)
> y = runif(10)
> df = data.frame( x, y )

# write it to a file
> write.table( df, 'test.txt', row.names = F, quote = F )

# read a data.frame from a file: 
> read.table( df, 'test.txt', header = T )

> df$x
 [1] -0.95343778 -0.63098637 -1.30646529  1.38906143  0.51703237 -0.02246754
 [7]  0.20583548  0.21530721  0.69087460  2.30610998
> df$y
 [1] 0.66658148 0.15355851 0.60098886 0.14284576 0.20408723 0.58271061
 [7] 0.05170994 0.83627336 0.76713317 0.95052671

> df$x = x
> df
            y           x
1  0.66658148 -0.95343778
2  0.15355851 -0.63098637
3  0.60098886 -1.30646529
4  0.14284576  1.38906143
5  0.20408723  0.51703237
6  0.58271061 -0.02246754
7  0.05170994  0.20583548
8  0.83627336  0.21530721
9  0.76713317  0.69087460
10 0.95052671  2.30610998

> df[-1,]
            y           x
2  0.15355851 -0.63098637
3  0.60098886 -1.30646529
4  0.14284576  1.38906143
5  0.20408723  0.51703237
6  0.58271061 -0.02246754
7  0.05170994  0.20583548
8  0.83627336  0.21530721
9  0.76713317  0.69087460
10 0.95052671  2.30610998

> df$x = NULL
> df 
            y
1  0.66658148
2  0.15355851
3  0.60098886
4  0.14284576
5  0.20408723
6  0.58271061
7  0.05170994
8  0.83627336
9  0.76713317
10 0.95052671

这篇关于如何删除R中数据框的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆