Skip to content

replace repeated id in a pedigree list

2 messages · Roberta Carriero, Rui Barradas

#
Hello,

Ok, try the following.


fun <- function(x, d, last){
     new <- last
     if(any(d)){
         ii <- which(d)
         for(i in ii){
             new <- new + 1
             old <- x$ID[i]
             x$ID[i] <- new
             x$FA_ID[x$FA_ID == old] <- new
             x$MO_ID[x$MO_ID == old] <- new
         }
     } else new <- FALSE
     list(new = new, DF = x)
}

lastid <- nrow(dat)
idx <- duplicated(dat$ID)
sp <- split(dat, dat$FAMID)
dup <- split(idx, dat$FAMID)
result <- vector("list", length(sp))

for(i in seq_along(sp)){
     ret <- fun(sp[[i]], dup[[i]], lastid)
     result[[i]] <- ret$DF
     if(ret$new) lastid <- ret$new
}
d2 <- do.call(rbind, result)
# check (should be zero)
sum(duplicated(d2$ID))


Hope this helps,

Rui Barradas
Em 30-10-2012 11:22, Roberta Carriero escreveu: