如何从R上的数据框中删除特定行

时间:2021-12-13 09:11:29

Basically I have a data frame with 4 columns and several rows (now it is not important how many exactly). The last column of the data frame is an index that ranges from 0 to 99. For first (50 * 1991) rows that index is 0, for the others (50 * 1991) rows the index is 1..e so on until to 99. I would like to modify my data frame, so that the first 550 (50 * 11) rows were eliminated each time the index in the fourth column changes. Hence, it would eliminate the first 550 rows when the index is 0..the first 550 rows when the index is 1..è so on up to 99. How could I do?

基本上我有一个包含4列和几行的数据框(现在确切地说有多少是不重要的)。数据框的最后一列是一个范围从0到99的索引。对于索引为0的第一(50 * 1991)行,对于其他(50 * 1991)行,索引为1..e,直到99.我想修改我的数据框,这样每当第四列中的索引发生变化时,就会删除前550(50 * 11)行。因此,当索引为0时,它将消除前550行。当索引为1时,前550行,等等达到99.我该怎么办?

I tried like this:

我试过这样的:

  for(m in 0:99){
  fitty[[1]]<-fitty[[1]][fitty[[1]][,4]==m][-(1:550),]
  }

Fitty[[1]] is the data frame. Really i have to do the same thing for fitty[[i]] with i in 1:5..but this is not a problem.

Fitty [[1]]是数据框。真的,我必须在1:5的时候和我一起为fitty [[i]]做同样的事情。但这不是问题。

1 个解决方案

#1


1  

There is no "remove a row" in R.

R中没有“删除一行”。

Instead what you do is re-save the same data.frame, but without the rows you do not want

相反,你所做的是重新保存相同的data.frame,但没有你不想要的行

 ## if `dat` is your data.frame
 dat <- dat[-i, ]

However, if you are going to do this iteratively, instead of dropping rows each iteration (ie, re-saving your data several times), it would be more efficient to add to a vector of "rows to drop" then dropping them all at once.

但是,如果要按迭代方式执行此操作,而不是每次迭代都删除行(即,多次重新保存数据),那么添加到“要删除的行”的向量然后将它们全部丢弃将更有效一旦。

#1


1  

There is no "remove a row" in R.

R中没有“删除一行”。

Instead what you do is re-save the same data.frame, but without the rows you do not want

相反,你所做的是重新保存相同的data.frame,但没有你不想要的行

 ## if `dat` is your data.frame
 dat <- dat[-i, ]

However, if you are going to do this iteratively, instead of dropping rows each iteration (ie, re-saving your data several times), it would be more efficient to add to a vector of "rows to drop" then dropping them all at once.

但是,如果要按迭代方式执行此操作,而不是每次迭代都删除行(即,多次重新保存数据),那么添加到“要删除的行”的向量然后将它们全部丢弃将更有效一旦。