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

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

问题描述

我有一个11列的数据集,每列超过1000行。列被标记为V1,V2,V11等。
我用c命令替换了对我更有用的东西。
我没有意识到第1行还包含每列的标签,我的实际数据从第2行开始。



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

解决方案

保留原始文件中的标签,如下所示:

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

如果您有列为x和y的列,可以这样解决:

  df $ x 
df $ y

如果要从数据中实际删除第一行.frame,你可以使用这样的负指数:

  df = df [-1,] 

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

  df $ x = NULL 

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

 #创建一个data.frame与10 ro ws 
> x = rnorm(10)
> y = runif(10)
> df = data.frame(x,y)

#将其写入文件
> write.table(df,'test.txt',row.names = F,quote = F)

#从文件中读取一个data.frame:
> 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
yx
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,]
yx
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 $ b $
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


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.

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)

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

df$x
df$y

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

df = df[-1,]

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

df$x = NULL

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天全站免登陆