0

How can I save the changes occurred in a data frame within a loop, after each iteration? By the following codes, I want to delete some rows from my data frame (df) which have equal value to 'v'. The codes works well, but the problem is finally, only the result of the last i value in the iterations affects the data frame!

for (i in 1:50){
  v <- i+ 450
      temp<- fn$sqldf("select count(V1) from df where V1='$v' ")
  if (temp[1,] < 1000){ 
    g <- temp[1,]
    c <- v
    print(paste("Topic number: ", c, "is less than 1000, with ", g, "records") )
    new_df<- df[df$V1 != v,]
  }

}
MASUMEH
  • 41
  • 6
  • This would work better if we had a reproducible example at hand. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Apr 07 '14 at 12:18

1 Answers1

1

A more idiomatic R way would be:

reduced <- subset(df, V1 > 450 & V1 <= 500)
count <- table(reduced$V1)
V1OK <- as.integer(names(count)[count<1000])
filtered <- subset(reduced, V1 %in% V1OK)

If you'd rather continue with an sql-centric perspective, your problem appears to be that in the creation of file3, you're generating this from new each iteration (I have to guess what file_new is). You could set up a flag for each row prior to the loop:

V1OK <- rep(FALSE, nrow(DF))

and update it within the loop with

V1OK <- V1OK | df$V1 !=v

and after the loop you could access

file_new[V1OK,]
Gavin Kelly
  • 2,374
  • 1
  • 10
  • 13
  • Thanks for your answer. Sorry for my mistake, I forgot to change file_new with df, which actually is my data frame's name.I tried your first solution and it gives me the data which i want to delete from my df. But, how can I have the data frame (df) with other data excluding these filtered data? – MASUMEH Apr 07 '14 at 15:22
  • You could try `df[!V1OK,]` if what I've given you is the logical opposite of what you were wanting? – Gavin Kelly Apr 07 '14 at 16:05
  • This gives me a data frame with 0 objects!Is there anything similar to %in% which indicates 'not included' then I can use it in the subset statement? { filtered <- subset(reduced, V1 {?} V1OK) – MASUMEH Apr 07 '14 at 16:22
  • opposite to `x %in% y` is `!(x %in% y)`, `!` being 'not'. See also `?match`, from which `%in%` is derived in case you need to tweak it further. – mathematical.coffee Apr 08 '14 at 03:21
  • Thanks a lot @mathematical.coffee. I've tried %nin% in Hmisc package, but it didn't give me the desired result! but your solution works well. And also thanks to Mr.Gavin kelly. – MASUMEH Apr 08 '14 at 03:54