简体   繁体   English

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

[英]How to delete specific rows from a data frame on R.

Basically I have a data frame with 4 columns and several rows (now it is not important how many exactly). 基本上,我有一个包含4列和几行的数据框(现在,精确多少行并不重要)。 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. 数据帧的最后一列是索引,范围从0到99。对于前(50 * 1991)行,索引为0,对于其他(50 * 1991)行,索引为1..e,依此类推直到99.我想修改我的数据框,以便每当第四列的索引更改时就消除前550(50 * 11)行。 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? 因此,它将消除索引为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. Fitty [[1]]是数据框。 Really i have to do the same thing for fitty[[i]] with i in 1:5..but this is not a problem. 真的,我必须为1:[5 ..]中的i做fitty [[i]]的相同操作,但这不是问题。

There is no "remove a row" in R . R没有"remove a row"

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. 但是,如果要迭代执行此操作,而不是每次迭代都删除行(即多次保存数据),则将矢量添加到“要删除的行”然后将它们全部删除的效率更高。一旦。

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

相关问题 R。 如何用日期替换数字或从数据框中删除带数字的行? - R. How to replace numbers with dates or remove rows with numbers from data frame? R.根据一秒内的值保留一个数据帧中的行 - R. Retaining rows from one data frame based on values in a second 如何从R中的面板数据框中删除具有唯一ID的行? - How to delete rows with a unique ID from a panel data frame in R? 如何从R中的数据框中选择具有特定日期的行 - How to select some rows with specific date from a data frame in R 从R中的数据框中提取特定行 - Extracting specific rows from the data frame in R 如何使用基于现有数据框的值创建新数据框,并使用R中的数字向量创建范围。 - How to create a new dataframe with values based on existing data frame and range from numeric vectors in R. R.如何将循环(for)结果附加到数据框中? - R. How to append loop (for) results into a Data Frame? R.如何在数据框中添加求和行 - R. How to add sum row in data frame 从 excel 文件中读取,其中包含空工作表作为列表并转换为 R 中的数据框。 bind_rows 中的错误 - Reading from an excel file having empty sheets as list and converting to a data frame in R. Error in bind_rows 如何从R中的数据帧的前n行删除条件下的行 - How to delete rows under a condition from first n rows of a data frame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM