[R] Remove records from a large dataframe

penguins catrsw at bas.ac.uk
Mon Oct 22 13:04:25 CEST 2012

Hi, I am trying to remove a series of records from a large dataframe. The
script I have written works fine but takes a long time to run. Can anyone
suggest a quicker way to do this?

Here is an example of the code I've written. The end result of this bit of
code would be a dataframe with any records relating to ID 1 or ID 4 removed:

id <-     c(1,1,1,1,2,2,2,2,2, 3,3,3, 4,4)
year <- c(1,1,1,2, 2,2,3,2,2, 2,3,4, 8,8)
age <-  c("Adult",NA,NA,NA, "Adult",NA,NA,NA, "Adult",
         NA,"Adult",NA, NA,"Adult")
dat <- data.frame(id, year, age)

#ID numbers for removal
bad<- data.frame(c(1,4))

good.id<- dat.id[!dat.id%in%remove.value]

#Combine all good ID numbers
if(exists("dat.2")){ rm(dat.2)}

for(i in good.id){

     if(!exists("dat.2")) {
      dat.2 <- lala } else {
      dat.2 <- rbind(dat.2, lala)

Many thanks in advance for any suggestions

View this message in context: http://r.789695.n4.nabble.com/Remove-records-from-a-large-dataframe-tp4646990.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list