Re: [R] Delete Block of Rows

From: Wacek Kusnierczyk <>
Date: Sun, 15 Jun 2008 17:36:53 +0200

nmarti wrote:
> I am trying to delete a section of rows from a data frame (based on no
> condition). Lets say my data frame has 200 rows and I want to delete rows 1
> through 25. How would I do this?
> I know x[ -1, ] deletes the first row (or any desired row). I tried using
> different variations of this, like x[ -c(1:25), ] but that didn't work.
what do you mean by 'didnt work'?
when you use negative indices, rows (or columns) from the input are skipped in the output. but to have a side effect of actually changing the data frame, you need an assignment. to modify your data frame, you need:

x = x[-c(<your spec>),]

while x[-c(<your spec>),] will (should) return a df with rows skipped, but x won't be changed.

> I
> also tried writting a few functions, for example:
> deleteRows <- function( data, s, e, ) {
> for( i in s:e )
> data[ -i, ]
> }
> deleteRows( ds, 1, 25 )
> But that didn't work either, it only deleted row 25
it didn't work, it didn't even delete row 25 from ds -- it *returned* a df with all rows of ds except row 25 (in a bad style, i'd say).

> (i'm new to writing
> functions).
consider reading the docs.

note that an implementation like

deleteRows = function(df, s, e) (df = df[-(s:e),,drop=FALSE])

won't work, because in r data frames are passed as arguments to functions by value (you are welcome to argue that this is insane).

vQ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sun 15 Jun 2008 - 16:18:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 15 Jun 2008 - 16:30:44 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive