Re: [R] (no subject)

From: <james.holtman_at_convergys.com>
Date: Tue 21 Jun 2005 - 03:04:38 EST

'rle' might be your friend. This will find the 'run of a sequence'

Here is some code working off the 'visit' data that you created.

# $Log$
x.1 <- matrix(visit, ncol=4) # your data x.rle <- apply(x.1, 1, rle) # compute 'rle' for each row Passed <- lapply(x.rle, function(x){ # now process each row see if it meets the criteria

    .len <- length(x$lengths)
    if (x$lengths[.len] > 1 && x$values[.len] == 1) return(TRUE) # last two passed

    else if (.len == 2){ # two sequences

        if (x$lengths[.len] == 1 && x$values[.len] == 1) return(TRUE) # only last passed

    }
    return(FALSE)
})
cbind(unlist(Passed), x.1) # put results in first column with the data

Jim



James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Convergys Labs james.holtman@convergys.com
+1 (513) 723-2929
                                                                                                                                           
                      r.ghezzo@staff.mcgill                                                                                                
                      .ca                          To:       r-help@stat.math.ethz.ch                                                      
                      Sent by:                     cc:                                                                                     
                      r-help-bounces@stat.m        Subject:  [R] (no subject)                                                              
                      ath.ethz.ch                                                                                                          
                                                                                                                                           
                                                                                                                                           
                      06/20/2005 11:58                                                                                                     
                                                                                                                                           





R friends,
I am using R 2.1.0 in a Win XP . I have a problem working with lists, probably I
do not understand how to use them.

Lets suppose that a set of patients visit a clinic once a year for 4 years on each visit a test, say 'eib' is performed with results 0 or 1 The patients do not all visit the clinic the 4 times but they missed a lot of visits.
The test is considered positive if it is positive at the last 2 visits of that
patient, or a more lenient definition, it is positive in the last visit, and
never before.
Otherwise it is Negative = always negative or is a YoYo = unstable = changes
from positive to negative.
So, if I codify the visits with codes 1,2,4,8 if present at year 1,2,3,4 and
similarly the tests positive I get the last2 list codifying the test code corresponding to the visits patterns possible, similarly the last1 list 20 here means NULL

nobs <- 400
# visits 0 1 2 3 4 5 6 7 8 9 last1 <- list((20),(1),(2),c(3,2),(4),c(5,4),c(6,4),c(7,6,4),(8),c(9,8), # visits 10 11 12 13 14 15

c(10,8),c(11,10,8),c(12,8),c(13,12,8),c(14,12,8),c(15,14,12,8)) # visits 0 1 2 3 4 5 6 7 8 9 last2 <- list((20),(20),(20),(3),(20),(5),(6),c(7,6),(20),(9),

#  visits      10    11      12    13       14       15
              (10),c(11,10),(12),c(13,12),c(14,12),c(15,14,12))
#
#     simulate the visits

#
visit <- rbinom(nobs,1,0.7)
eib <- visit
#
#     simulate a positive test at a given visit
#

eib <- ifelse(runif(nobs) > 0.7,visit,0)
#
#     create the codes
#

viskode <- matrix(visit,ncol=4) %*% c(1,2,4,8) eibkode <- matrix(eib,ncol=4) %*% c(1,2,4,8) #
# this is the brute force method, slow, of computing the Results according to
# the 2 definitions above. Add 16 to the test kode to signify YoYos, Exactly
# 16 will be the negatives
#
 eibnoyoyo <- eibkode+16
 eiblst2 <- eibkode+16
 for(i in 1:nobs){
   if(eibkode[i] %in% last1[[viskode[i]+1]])

      eibnoyoyo[i] <- eibkode[i]
   if(eibkode[i] %in% last2[[viskode[i]+1]])

      eiblast2[i] <- eibkode[i]
 }

#
#    why is that these statements do not work?
#

eeibnoyoyo <- eeiblst2 <- rep(0,nobs)
eeibnoyoyo <- ifelse(eibkode %in% last1[viskode+1],eibkode,eibkode+16) eeiblast2 <- ifelse(eibkode %in% last2[viskode+1],eibkode,eibkode+16) #
table(viskode,eibkode)
table(viskode,eibnoyoyo)
table(viskode,eiblast2)
#

# these two tables must be diagonal!!
#
table(eibnoyoyo,eeibnoyoyo)
table(eiblast2,eeiblast2)
#
Thanks for any help
Heberto Ghezzo
McGill University
Canada

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 21 03:14:17 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:54 EST