[英]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.