Cajira
Cajira

Reputation: 47

For Loop in R - deleting all rows which match by one variable

I'm trying to completely delete rows in a dataset for cases with matching variables (case ID) with the help of this function I wrote:

del_row_func <- function(x){
  for(i in 1:length(x$FALL_ID)){
   for(j in 1:length(x$FALL_ID)){
     if(x$FALL_ID[i] == x$FALL_ID[j] & i != j){
       x[-i, ]
      }
    }
  }  
}

Anybody have an idea, why it doesn't work?

Upvotes: 0

Views: 247

Answers (1)

josliber
josliber

Reputation: 44309

The reason your code didn't work was that you weren't modifying or returning x. However, there is a better way to remove all rows with a duplicated ID:

dat = data.frame(FALL_ID = c(1, 2, 2, 3), y = 1:4)
dat
#   FALL_ID y
# 1       1 1
# 2       2 2
# 3       2 3
# 4       3 4
dat[!duplicated(dat$FALL_ID) & !duplicated(dat$FALL_ID, fromLast=T),]
#   FALL_ID y
# 1       1 1
# 4       3 4

Upvotes: 2

Related Questions