删除R中的行时,如何自动缩小R数据帧中的行号 [英] How to automatically shrink down row numbers in R data frame when removing rows in R

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

问题描述



我有一个名为mydata的数据集,我从文本导入了一个数据集文件使用 R 。数据框有大约200行10列。



我通过使用以下方式删除了行号3,7,9,199:

  mydata<  -  mydata [-c(3,7,9,199),] 

当我运行这个命令时,行3,7,9,199从列表中删除,但行号不会自动缩小到196,但是保持在200。感觉像某种程度上这些行号是附加到每个行作为数据帧的一部分?



如何解决这个问题?



更令我困惑的是当我导入文本文件时 R Studio,我没有任何问题。 (当我运行上面的命令时,我看到了196)。但是,当使用 R 时,我无法更改与列表中实际行数匹配的数据帧中的行号。



可以任何人请告诉我如何解决这个问题?

解决方案

你可以简单地做:

  rownames(mydata)<  -  NULL 

执行子集。



例如:

 > mydata = data.frame(a = 1:10,b = 11:20)
> mydata = mydata [-c(6,8)]]
> mydata
ab
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
7 7 17
9 9 19
10 10 20
> rownames(mydata)< - NULL
> mydata
ab
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 7 17
7 9 19
8 10 20


I'm having a difficulty properly shrinking down the row numbers in a data frame.

I have a data set named "mydata" which I imported from a text file using R. The data frame has about 200 rows with 10 columns.

I removed the row number 3, 7, 9, 199 by using:

mydata <- mydata[-c(3, 7, 9, 199),]

When I run this command, the row 3,7,9,199 are gone from the list but the row number doesn't automatically shrink down to 196, but stays at 200. I feel like somehow these row numbers are attached to each "row" as part of the dataframe?

How do I fix this problem?

What puzzles me even more is that when I import the textfile using R Studio, I don't have any problem. (I see 196 when I run the above command). But when using R, I can't change the row number in a dataframe that matches the actual number of rows in the list.

Can anyone please tell me how to fix this??

解决方案

You can simply do:

rownames(mydata) <- NULL

after performing the subsetting.

For example:

> mydata = data.frame(a=1:10, b=11:20)
> mydata = mydata[-c(6, 8), ]
> mydata
    a  b
1   1 11
2   2 12
3   3 13
4   4 14
5   5 15
7   7 17
9   9 19
10 10 20
> rownames(mydata) <- NULL
> mydata
   a  b
1  1 11
2  2 12
3  3 13
4  4 14
5  5 15
6  7 17
7  9 19
8 10 20

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

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