Hello, I am a student from Liverpool UK, working with some genetic data using R. I got a data.frame with about 250.000 rows and using a for loop to delete certain rows using the following code: x <----- is a data.frame x = x[-5,] ---> deletes row 5 x = x[-10,] ---> delets row 10 x = x[-i,] --> deletes row i etc... This takes ages for some reason. Isnt there a quicker way to do this?! Cheers, Sven -- View this message in context: http://r.789695.n4.nabble.com/Delete-row-takes-ages-alternative-tp3656949p3656949.html Sent from the R help mailing list archive at Nabble.com.
On Jul 9, 2011, at 7:02 PM, sven wrote:> Hello, > I am a student from Liverpool UK, working with some genetic data > using R. > > I got a data.frame with about 250.000 rows and using a for loop to > delete > certain rows using the following code: > > x <----- is a data.frame > x = x[-5,] ---> deletes row 5 > x = x[-10,] ---> delets row 10 > x = x[-i,] --> deletes row i > etc... > > This takes ages for some reason. Isnt there a quicker way to do this?!Two ways that would avoid repeated copying of medium size dataframes: x <- x[-c(5,10, i) , ] drpidx <- -c(5,10, i) x <- x[-i, ] -- David.> Cheers, > Sven > > > -- > View this message in context: http://r.789695.n4.nabble.com/Delete-row-takes-ages-alternative-tp3656949p3656949.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
On 10/07/11 12:12, David Winsemius wrote: <SNIP> Uhhh, you meant> drpidx <- -c(5,10, i) > x <- x[-drpidx, ] >cheers, Rolf Turner
On Jul 9, 2011, at 9:37 PM, Rolf Turner wrote:> On 10/07/11 12:12, David Winsemius wrote: > > <SNIP> > > Uhhh, you meant >> drpidx <- -c(5,10, i) >> x <- x[-drpidx, ] >> >Yes, I certainly did. Thanks for the correction.> cheers, > > Rolf Turner >David Winsemius, MD West Hartford, CT
On Jul 9, 2011, at 11:42 PM, David Winsemius wrote:> > On Jul 9, 2011, at 9:37 PM, Rolf Turner wrote: > >> On 10/07/11 12:12, David Winsemius wrote: >> >> <SNIP> >> >> Uhhh, you meant >>> drpidx <- -c(5,10, i) >>> x <- x[-drpidx, ] >>> >> > Yes, I certainly did. Thanks for the correction.A little bird has tweeted that I might NOT have wanted to accept that" > i <- 5 > del.idx <- c(4,7,i) # Notice no minus sign. > (1:10)[-del.idx] [1] 1 2 3 6 8 9 10 Double negation turns the result positive: > del.idx <- -c(4,7,i) > (1:10)[-del.idx] # minus a minus [1] 4 7 5> >> cheers, >> >> Rolf Turner-- David Winsemius, MD West Hartford, CT