简体   繁体   中英

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). 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?

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.

There is no "remove a row" in R .

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

 ## 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM