[R] Data transformation to list for event occurence

arun smartpink111 at yahoo.com
Tue Nov 12 23:13:08 CET 2013



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.




More information about the R-help mailing list