Rorita_Tai
Rorita_Tai

Reputation: 33

search certain number vector in R

I have a list of number vector like this:

[[1]]
[1] 0 1

[[2]]
[1] 0 1

[[3]]
[1] 1 0

[[4]]
[1] 1 0

Im new in R,can anyone tell me what function i can use to count The block number of the last occurrence

number of the last occurrence  "[0,0]"=0
number of the last occurrence  "[0,1]"=2
number of the last occurrence  "[1,0]"=4
number of the last occurrence  "[1,1]"=0

Thanks in advance!

Upvotes: 0

Views: 80

Answers (3)

Rich Scriven
Rich Scriven

Reputation: 99361

Here's another possibility

## your list
x <- list(c(0,1), c(0,1), c(1,0), c(1,0))
## list of vectors for comparison
comp <- list(c(0,0), c(0,1), c(1,0), c(1,1))

duplicated and identical will help here. First we determine the identical elements, then we find their duplicates and replace the index values with the index of the duplicate(s).

m <- mapply(identical, x, comp)
replace(as.numeric(m), m, which(duplicated(x)))
#[1] 0 2 4 0

Upvotes: 1

akrun
akrun

Reputation: 887691

Another variation of @hrbrmstr's lastBlock is:

 lastBlock <- function(lst, val){
   do.call(pmax, as.list(c(0, which(vapply(lst,
                       function(x) all(x==val), logical(1L))))))
    }
lastBlock(dat, c(1,0))
#[1] 4
lastBlock(dat, c(0,0))
#[1] 0
lastBlock(dat, c(0,1))
#[1] 2

Update

If you want to search for more than one set of val

 lastBlock1 <- function(lst, searchDat){
    s1 <- sapply(lst, paste, collapse=",")
    s2 <- do.call(paste, c(searchDat, list(sep=',')))
    indx <- outer(s2, s1, `==`)
    res <- do.call(`pmax`, as.data.frame(col(indx)*indx))
    cat(paste0('number of the last occurence ', "'[", s2, "]' = ",
                   res, collapse="\n"),'\n')
  }     

   lastBlock1(dat,dat2)
   #number of the last occurence '[0,0]' = 0
   #number of the last occurence '[1,0]' = 4
   #number of the last occurence '[0,1]' = 2
   #number of the last occurence '[1,1]' = 0 

Or a variation of the above approach using match (which would be faster). The credit goes to @alexis_laz

  lastBlock2 <- function(lst, searchDat){
    s1 <- sapply(lst, paste, collapse=",")
    s2 <- do.call(paste, c(searchDat, list(sep=',')))
    res <- (length(s1)+1) - match(s2, rev(s1), nomatch=length(s1)+1)
    cat(paste0('number of the last occurence ', "'[", s2, "]' = ",
                   res, collapse="\n"),'\n')
  }     

 lastBlock2(dat,dat2)
 #number of the last occurence '[0,0]' = 0
 #number of the last occurence '[1,0]' = 4
 #number of the last occurence '[0,1]' = 2
 #number of the last occurence '[1,1]' = 0 

data

dat <- list(c(0,1), c(0,1), c(1,0), c(1,0)) 
dat2 <- expand.grid(rep(list(0:1),2))

Upvotes: 2

hrbrmstr
hrbrmstr

Reputation: 78832

Nothing is built in (AFAIK) but it's a pretty simple function to build:

dat <- list(c(0,1), c(0,1), c(1,0), c(1,0))

lastBlock <- function(val) {
  val <- tail(which(sapply(dat, function(x) all(x==val))), 1)
  ifelse(length(val)==0, 0, val)
}

lastBlock(c(0,0))
## [1] 0

lastBlock(c(0,1))
## [1] 2

lastBlock(c(1,0))
## [1] 4

lastBlock(c(1,1))
## [1] 0

Upvotes: 3

Related Questions