简体   繁体   中英

removing specific rows from a data frame within a loop in r

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,]
  }

}

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,]

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