Skip to content
Prev 308930 / 398503 Next

How to pick colums from a ragged array?

Hello,

Using one of Arun's ideas, some post ago, this new function returns a 
logical index into id.d of the rows that should be _removed_, hence rm1 
and rm2. I think



getRepLogical <- function(x, first = TRUE){
     fun <- if(first) head else tail
     dte <- tapply(x[,2], x[,1], FUN = function(x) duplicated(fun(x, 2)))
     len <- tapply(x[,2], x[,1], FUN = length)
     lst <- lapply(seq_along(dte), function(i) c(dte[[i]], rep(FALSE, 
if(len[[i]] > 2) len[[i]] - 2 else 0)))
     lst <- if(first) lst else lapply(lst, rev)
     i1 <- unlist(lst)
     dg <- tapply(x[,3], x[,1], FUN = function(x) !duplicated(fun(x, 2)))
     lst <- lapply(seq_along(dte), function(i) c(dg[[i]], rep(FALSE, 
if(len[[i]] > 2) len[[i]] - 2 else 0)))
     lst <- if(first) lst else lapply(lst, rev)
     i2 <- unlist(lst)
     i1 & i2
}

rm1 <- getRepLogical(id.d)
rm2 <- getRepLogical(id.d, first = FALSE)

id.d[rm1, ]
id.d[rm2, ]

id.d$INCLUDE <- !(rm1 | rm2)


Hope this helps,

Rui Barradas
Em 24-10-2012 16:41, Stuart Leask escreveu: