Re: [R] Calculating number of elapsed days from starting date

2012-09-27 Thread arun
Hi,

You can also use either of these:
data$date <- as.Date(data$date,format="%m/%d/%y")
data$Days<-as.vector(sapply(lapply(split(data,data$Person),`[`,2),function(x) 
difftime(x[,1],x[1,],units="days")))

#or

data$Days<-as.vector(sapply(lapply(split(data,data$Person),`[`,2),function(x) 
x[,1]-x[1,]))

#or

data$Days<-unlist(lapply(lapply(split(data,data$Person),`[`,2),function(x) 
difftime(x[,1],x[1,],units="days")))
data
data
#  Person   date Days
#1    bob 2000-01-01    0
#2    bob 2000-01-02    1
#3    bob 2000-01-03    2
#4   dave 2000-01-07    0
#5   dave 2000-01-08    1
#6   dave 2000-01-10    3
#7  kevin 2000-01-02    0
#8  kevin 2000-01-03    1
#9  kevin 2000-01-04    2
A.K.


- Original Message -
From: Marcel Curlin 
To: r-help@r-project.org
Cc: 
Sent: Thursday, September 27, 2012 1:16 AM
Subject: [R] Calculating number of elapsed days from starting date

Hi 
I have data for events in rows, with columns for person and date. Each
person may have more than one event;

tC <- textConnection("
Person    date
bob    1/1/00
bob    1/2/00
bob    1/3/00
dave    1/7/00
dave    1/8/00
dave    1/10/00
kevin    1/2/00
kevin    1/3/00
kevin    1/4/00
")
data <- read.table(header=TRUE, tC)
close.connection(tC)
rm(tC)

I would like to add a new column to my dataframe containing the calculated
number of elapsed days from the starting date for each person. So the new
dataframe would read

Person    date    Days
bob    1/1/00    0
bob    1/2/00    1
bob    1/3/00    2
dave    1/7/00    0
dave    1/8/00    1
dave    1/10/00    3
kevin    1/2/00    0
kevin    1/3/00    1
kevin    1/4/00    2

Not sure how to do this, tried looking through the forum but didn't find
anything that seemed to apply. Suggestions appreciated.




--
View this message in context: 
http://r.789695.n4.nabble.com/Calculating-number-of-elapsed-days-from-starting-date-tp4644333.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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@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.


Re: [R] Calculating number of elapsed days from starting date

2012-09-27 Thread Berend Hasselman

On 27-09-2012, at 07:16, Marcel Curlin  wrote:

> Hi 
> I have data for events in rows, with columns for person and date. Each
> person may have more than one event;
> 
> tC <- textConnection("
> Persondate
> bob   1/1/00
> bob   1/2/00
> bob   1/3/00
> dave  1/7/00
> dave  1/8/00
> dave  1/10/00
> kevin 1/2/00
> kevin 1/3/00
> kevin 1/4/00
> ")
> data <- read.table(header=TRUE, tC)
> close.connection(tC)
> rm(tC)
> 
> I would like to add a new column to my dataframe containing the calculated
> number of elapsed days from the starting date for each person. So the new
> dataframe would read
> 
> PersondateDays
> bob   1/1/00  0
> bob   1/2/00  1
> bob   1/3/00  2
> dave  1/7/00  0
> dave  1/8/00  1
> dave  1/10/00 3
> kevin 1/2/00  0
> kevin 1/3/00  1
> kevin 1/4/00  2
> 
> Not sure how to do this, tried looking through the forum but didn't find
> anything that seemed to apply. Suggestions appreciated.


You could do this

DF$realdate <- as.Date(as.character(DF$date),format="%m/%d/%y")
DF$Days <- ave(as.numeric(DF$realdate), by=DF$Person, FUN=function(x) x-x[1])

If you don't want the as.character then you should add stringsAsFactors=FALSE 
to the read.table argument list.

Berend

__
R-help@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.


Re: [R] Calculating number of elapsed days from starting date

2012-09-27 Thread Rolf Turner

On 27/09/12 17:16, Marcel Curlin wrote:

Hi
I have data for events in rows, with columns for person and date. Each
person may have more than one event;

tC <- textConnection("
Person  date
bob 1/1/00
bob 1/2/00
bob 1/3/00
dave1/7/00
dave1/8/00
dave1/10/00
kevin   1/2/00
kevin   1/3/00
kevin   1/4/00
")
data <- read.table(header=TRUE, tC)
close.connection(tC)
rm(tC)

I would like to add a new column to my dataframe containing the calculated
number of elapsed days from the starting date for each person. So the new
dataframe would read

Person  dateDays
bob 1/1/00  0
bob 1/2/00  1
bob 1/3/00  2
dave1/7/00  0
dave1/8/00  1
dave1/10/00 3
kevin   1/2/00  0
kevin   1/3/00  1
kevin   1/4/00  2

Not sure how to do this, tried looking through the forum but didn't find
anything that seemed to apply. Suggestions appreciated.


X <- data
X$date <- with(X,as.Date(date,format="%m/%d/%y"))
X$Days <- unlist(with(X,tapply(date,Person,function(x){x-x[1]})))

And then if you *really* want to keep that shaganappi date format:

X$date <- data$date
X
# Giving:
Persondate Days
1bob  1/1/000
2bob  1/2/001
3bob  1/3/002
4   dave  1/7/000
5   dave  1/8/001
6   dave 1/10/003
7  kevin  1/2/000
8  kevin  1/3/001
9  kevin  1/4/002

BTW:  You really *shouldn't* call your data "data".  See:

fortune("dog")

cheers,

Rolf Turner

__
R-help@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] Calculating number of elapsed days from starting date

2012-09-26 Thread Marcel Curlin
Hi 
I have data for events in rows, with columns for person and date. Each
person may have more than one event;

tC <- textConnection("
Person  date
bob 1/1/00
bob 1/2/00
bob 1/3/00
dave1/7/00
dave1/8/00
dave1/10/00
kevin   1/2/00
kevin   1/3/00
kevin   1/4/00
")
data <- read.table(header=TRUE, tC)
close.connection(tC)
rm(tC)

I would like to add a new column to my dataframe containing the calculated
number of elapsed days from the starting date for each person. So the new
dataframe would read

Person  dateDays
bob 1/1/00  0
bob 1/2/00  1
bob 1/3/00  2
dave1/7/00  0
dave1/8/00  1
dave1/10/00 3
kevin   1/2/00  0
kevin   1/3/00  1
kevin   1/4/00  2

Not sure how to do this, tried looking through the forum but didn't find
anything that seemed to apply. Suggestions appreciated.




--
View this message in context: 
http://r.789695.n4.nabble.com/Calculating-number-of-elapsed-days-from-starting-date-tp4644333.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.