Skip to content

Data transformation to list for event occurence

3 messages · Anindya Sankar Dey, arun, William Dunlap

#
Hi Anindya,

You may try:
dat1 <- read.table(text="ID?? Week??? Event_Occurence
A 1 0
A 2 0
A 3 1
A 4 0
B 1 1
B 2 0
B 3 0
B 4 1",sep="",header=TRUE,stringsAsFactors=FALSE)

?with(dat1,tapply(as.logical(Event_Occurence),ID,FUN=which ))
#or
lapply(split(dat1,dat1$ID),function(x) which(!!x[,3]))
A.K.
On Tuesday, November 12, 2013 4:58 PM, Anindya Sankar Dey <anindya55 at gmail.com> wrote:
Hi,

Say I have a following data

ID?  Week? ? Event_Occurence
A 1 0
A 2 0
A 3 1
A 4 0
B 1 1
B 2 0
B 3 0
B 4 1

that whether an individual experienced an event in a particular week.

I wish to create list such as the first element of the list will be a
vector listing the week number when the event has occurred for A, followed
by that of B.

Can you help creating this?
#
Or,

f3 <- function (dat1)  {
    i <- dat1$Event_Occurence == 1
    split(dat1$Week[i], dat1$ID[i])
}

in addition to the previously mentioned
f1 <- function(dat1) {
    with(dat1,tapply(as.logical(Event_Occurence),ID,FUN=which ))
}
f2 <- function(dat1){
     lapply(split(dat1,dat1$ID),function(x) which(!!x[,3]))
}

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com