[R] Data transformation to list for event occurence

William Dunlap wdunlap at tibco.com
Wed Nov 13 17:13:18 CET 2013


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


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of arun
> Sent: Tuesday, November 12, 2013 2:13 PM
> To: R help
> Subject: Re: [R] Data transformation to list for event occurence
> 
> 
> 
> 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?
> 
> --
> Anindya Sankar Dey
> 
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list