Re: [R] Computing row differences in new columns

2011-03-21 Thread Roberto Lodeiro Muller

-Original Message-
From: Roberto Lodeiro Muller roberto.mul...@doctor.com
To: roberto.mul...@doctor.com
Sent: Mon, Mar 21, 2011 3:37 pm
Subject: Re: [R] Computing row differences in new columns


Sorry, my data appeared badly formatted to me, so I converted it to plain text:
 
And just to clarify, for each subject in the first row it should appear the 
difference to the next row, so that the last entry on each subject would be a 
NA.
 
Thanks again for your help
 
Roberto
 
SUBJECTDATE   RESULT DateDiff   ResultDiff
10751  22-Jul-03  3.5
10751  13-Feb-04  1.3
10751  20-Aug-04  1.6
10751  08-Mar-05  1.7
10751  30-Aug-05  1.6
10751  21-Feb-06  1.3
10751  31-Aug-06  1.2
10751  27-Feb-07  1.5
10751  29-Aug-07  1  
10752  29-Jul-03  5.9   
10752  24-Feb-04  5  
10752  25-Aug-04  3.6
10752  11-Mar-05  5.1
10752  18-Sep-05  2.2
10752  23-Feb-06  3.1
10752  24-Aug-06  3.7
10752  27-Feb-07  6  

 


 

-Original Message-
From: Roberto Lodeiro Muller roberto.mul...@doctor.com
To: r-help@r-project.org
Sent: Mon, Mar 21, 2011 3:23 pm
Subject: [R] Computing row differences in new columns



i 
I have the following columns with dates and results, sorted by subject and 
date. 
'd like to compute the differences in dates and results for each patient, based 
n the previous row. Obviously the last entry for each subject should be a NA.
 Which would be the best way to accomplished that ?
I guess questions like that have been already answered a thousand times, so I 
pologize for asking one more time.
Thanks
Roberto





SUBJECT
ate
esult
ateDiff
esultDiff
10751
2-Jul-03
.5


0751
3-Feb-04
.3

10751
0-Aug-04
.6

10751
8-Mar-05
.7

10751
0-Aug-05
.6

10751
1-Feb-06
.3

10751
1-Aug-06
.2

10751
7-Feb-07
.5

10751
9-Aug-07


10752
9-Jul-03
.9

10752
4-Feb-04


10752
5-Aug-04
.6

10752
1-Mar-05
.1

10752
8-Sep-05
.2

10752
3-Feb-06
.1

10752
4-Aug-06
.7

10752
7-Feb-07



[[alternative HTML version deleted]]
__
-h...@r-project.org mailing list
ttps://stat.ethz.ch/mailman/listinfo/r-help
LEASE do read the posting guide http://www.R-project.org/posting-guide.html
nd provide commented, minimal, self-contained, reproducible code.



[[alternative HTML version deleted]]

__
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] Computing row differences in new columns

2011-03-21 Thread Alexander Engelhardt

(mods: can you un-flag me if I am still flagged?)

Hi,

First, I would convert the DATE column to an integer. I found this by 
quick googling, perhaps as.Date is your weapon of choice:

https://stat.ethz.ch/pipermail/r-help/2008-August/169870.html

For the column generation I think you will have to use a for-loop here:

yourdata$ResultDiff - 0
yourdata$DateDiff - 0

for( i in 1:nrow(yourdata) ){
	yourdata$ResultDiff[i] - yourdata$RESULT[i] - yourdata$RESULT[i+1] # 
should automatically NA the last cell

yourdata$DateDiff[i] - yourdata$datenumber[i] - 
yourdata$datenumber[i+1]
}

where datenumber is your integer column of the date.

-- Alex


Am 21.03.2011 20:38, schrieb Roberto Lodeiro Muller:


-Original Message-
From: Roberto Lodeiro Mullerroberto.mul...@doctor.com
To: roberto.mul...@doctor.com
Sent: Mon, Mar 21, 2011 3:37 pm
Subject: Re: [R] Computing row differences in new columns


Sorry, my data appeared badly formatted to me, so I converted it to plain text:

And just to clarify, for each subject in the first row it should appear the 
difference to the next row, so that the last entry on each subject would be a 
NA.

Thanks again for your help

Roberto

SUBJECTDATE   RESULT DateDiff   ResultDiff
10751  22-Jul-03  3.5
10751  13-Feb-04  1.3
10751  20-Aug-04  1.6
10751  08-Mar-05  1.7
10751  30-Aug-05  1.6
10751  21-Feb-06  1.3
10751  31-Aug-06  1.2
10751  27-Feb-07  1.5
10751  29-Aug-07  1
10752  29-Jul-03  5.9
10752  24-Feb-04  5
10752  25-Aug-04  3.6
10752  11-Mar-05  5.1
10752  18-Sep-05  2.2
10752  23-Feb-06  3.1
10752  24-Aug-06  3.7
10752  27-Feb-07  6


__
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] Computing row differences in new columns

2011-03-21 Thread Dennis Murphy
Hi:

Here's one way. Calling your data frame below d,

# Function to compute consecutive differences
dif - function(x) c(diff(x), NA)

# Make sure that DATE is a Date variable
d$DATE - as.Date(d$DATE, format = '%d-%b-%y')

# Apply the function to each individual
ddply(d, 'SUBJECT', transform, diffDate = dif(DATE), diffResult =
dif(RESULT))

   SUBJECT   DATE RESULT diffDate diffResult
110751 2003-07-223.5  206   -2.2
210751 2004-02-131.3  1890.3
310751 2004-08-201.6  2000.1
410751 2005-03-081.7  175   -0.1
510751 2005-08-301.6  175   -0.3
610751 2006-02-211.3  191   -0.1
710751 2006-08-311.2  1800.3
810751 2007-02-271.5  183   -0.5
910751 2007-08-291.0   NA NA
10   10752 2003-07-295.9  210   -0.9
11   10752 2004-02-245.0  183   -1.4
12   10752 2004-08-253.6  1981.5
13   10752 2005-03-115.1  191   -2.9
14   10752 2005-09-182.2  1580.9
15   10752 2006-02-233.1  1820.6
16   10752 2006-08-243.7  1872.3
17   10752 2007-02-276.0   NA NA

If you want the NA first (which is reasonable), flip the NA and diff(x) in
the function definition.

HTH,
Dennis



On Mon, Mar 21, 2011 at 12:38 PM, Roberto Lodeiro Muller 
roberto.mul...@doctor.com wrote:


 -Original Message-
 From: Roberto Lodeiro Muller roberto.mul...@doctor.com
 To: roberto.mul...@doctor.com
 Sent: Mon, Mar 21, 2011 3:37 pm
 Subject: Re: [R] Computing row differences in new columns


 Sorry, my data appeared badly formatted to me, so I converted it to plain
 text:

 And just to clarify, for each subject in the first row it should appear the
 difference to the next row, so that the last entry on each subject would be
 a NA.

 Thanks again for your help

 Roberto

 SUBJECTDATE   RESULT DateDiff   ResultDiff
 10751  22-Jul-03  3.5
 10751  13-Feb-04  1.3
 10751  20-Aug-04  1.6
 10751  08-Mar-05  1.7
 10751  30-Aug-05  1.6
 10751  21-Feb-06  1.3
 10751  31-Aug-06  1.2
 10751  27-Feb-07  1.5
 10751  29-Aug-07  1
 10752  29-Jul-03  5.9
 10752  24-Feb-04  5
 10752  25-Aug-04  3.6
 10752  11-Mar-05  5.1
 10752  18-Sep-05  2.2
 10752  23-Feb-06  3.1
 10752  24-Aug-06  3.7
 10752  27-Feb-07  6






 -Original Message-
 From: Roberto Lodeiro Muller roberto.mul...@doctor.com
 To: r-help@r-project.org
 Sent: Mon, Mar 21, 2011 3:23 pm
 Subject: [R] Computing row differences in new columns



 i
 I have the following columns with dates and results, sorted by subject and
 date.
 'd like to compute the differences in dates and results for each patient,
 based
 n the previous row. Obviously the last entry for each subject should be a
 NA.
  Which would be the best way to accomplished that ?
 I guess questions like that have been already answered a thousand times, so
 I
 pologize for asking one more time.
 Thanks
 Roberto





 SUBJECT
 ate
 esult
 ateDiff
 esultDiff
 10751
 2-Jul-03
 .5


 0751
 3-Feb-04
 .3

 10751
 0-Aug-04
 .6

 10751
 8-Mar-05
 .7

 10751
 0-Aug-05
 .6

 10751
 1-Feb-06
 .3

 10751
 1-Aug-06
 .2

 10751
 7-Feb-07
 .5

 10751
 9-Aug-07


 10752
 9-Jul-03
 .9

 10752
 4-Feb-04


 10752
 5-Aug-04
 .6

 10752
 1-Mar-05
 .1

 10752
 8-Sep-05
 .2

 10752
 3-Feb-06
 .1

 10752
 4-Aug-06
 .7

 10752
 7-Feb-07



[[alternative HTML version deleted]]
 __
 -h...@r-project.org mailing list
 ttps://stat.ethz.ch/mailman/listinfo/r-help
 LEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 nd provide commented, minimal, self-contained, reproducible code.



[[alternative HTML version deleted]]

 __
 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.


[[alternative HTML version deleted]]

__
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] Computing row differences in new columns

2011-03-21 Thread David Winsemius


On Mar 21, 2011, at 3:38 PM, Roberto Lodeiro Muller wrote:



-Original Message-
From: Roberto Lodeiro Muller roberto.mul...@doctor.com
To: roberto.mul...@doctor.com
Sent: Mon, Mar 21, 2011 3:37 pm
Subject: Re: [R] Computing row differences in new columns


Sorry, my data appeared badly formatted to me, so I converted it to  
plain text:


And just to clarify, for each subject in the first row it should  
appear the difference to the next row, so that the last entry on  
each subject would be a NA.


Thanks again for your help

Roberto

SUBJECTDATE   RESULT DateDiff   ResultDiff
10751  22-Jul-03  3.5
10751  13-Feb-04  1.3
10751  20-Aug-04  1.6
10751  08-Mar-05  1.7
10751  30-Aug-05  1.6
10751  21-Feb-06  1.3
10751  31-Aug-06  1.2
10751  27-Feb-07  1.5
10751  29-Aug-07  1
10752  29-Jul-03  5.9
10752  24-Feb-04  5
10752  25-Aug-04  3.6
10752  11-Mar-05  5.1
10752  18-Sep-05  2.2
10752  23-Feb-06  3.1
10752  24-Aug-06  3.7
10752  27-Feb-07  6


dat$dt - as.Date(dat$DATE, format=%d-%b-%y)
dat$diffdt - c(diff(dat$dt), NA)
dat$diffRES - c(diff(dat$RES), NA)


--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] Computing row differences in new columns

2011-03-21 Thread Henrique Dallazuanna
Try this:

dat$DATE - as.Date(dat$DATE, %d-%b-%y)
dat - cbind(dat, lapply(mapply(tapply, MoreArgs = list(INDEX =
c(dat$SUBJECT, unique(dat$SUBJECT)), FUN = diff),

lapply(dat[,2:3], c, unique(dat$SUBJECT) / NA), SIMPLIFY = FALSE),
unlist))


On Mon, Mar 21, 2011 at 4:38 PM, Roberto Lodeiro Muller
roberto.mul...@doctor.com wrote:

 -Original Message-
 From: Roberto Lodeiro Muller roberto.mul...@doctor.com
 To: roberto.mul...@doctor.com
 Sent: Mon, Mar 21, 2011 3:37 pm
 Subject: Re: [R] Computing row differences in new columns


 Sorry, my data appeared badly formatted to me, so I converted it to plain 
 text:

 And just to clarify, for each subject in the first row it should appear the 
 difference to the next row, so that the last entry on each subject would be a 
 NA.

 Thanks again for your help

 Roberto

 SUBJECT    DATE       RESULT     DateDiff   ResultDiff
 10751      22-Jul-03  3.5
 10751      13-Feb-04  1.3
 10751      20-Aug-04  1.6
 10751      08-Mar-05  1.7
 10751      30-Aug-05  1.6
 10751      21-Feb-06  1.3
 10751      31-Aug-06  1.2
 10751      27-Feb-07  1.5
 10751      29-Aug-07  1
 10752      29-Jul-03  5.9
 10752      24-Feb-04  5
 10752      25-Aug-04  3.6
 10752      11-Mar-05  5.1
 10752      18-Sep-05  2.2
 10752      23-Feb-06  3.1
 10752      24-Aug-06  3.7
 10752      27-Feb-07  6






 -Original Message-
 From: Roberto Lodeiro Muller roberto.mul...@doctor.com
 To: r-help@r-project.org
 Sent: Mon, Mar 21, 2011 3:23 pm
 Subject: [R] Computing row differences in new columns



 i
 I have the following columns with dates and results, sorted by subject and 
 date.
 'd like to compute the differences in dates and results for each patient, 
 based
 n the previous row. Obviously the last entry for each subject should be a NA.
  Which would be the best way to accomplished that ?
 I guess questions like that have been already answered a thousand times, so I
 pologize for asking one more time.
 Thanks
 Roberto





 SUBJECT
 ate
 esult
 ateDiff
 esultDiff
 10751
 2-Jul-03
 .5


 0751
 3-Feb-04
 .3

 10751
 0-Aug-04
 .6

 10751
 8-Mar-05
 .7

 10751
 0-Aug-05
 .6

 10751
 1-Feb-06
 .3

 10751
 1-Aug-06
 .2

 10751
 7-Feb-07
 .5

 10751
 9-Aug-07


 10752
 9-Jul-03
 .9

 10752
 4-Feb-04


 10752
 5-Aug-04
 .6

 10752
 1-Mar-05
 .1

 10752
 8-Sep-05
 .2

 10752
 3-Feb-06
 .1

 10752
 4-Aug-06
 .7

 10752
 7-Feb-07



    [[alternative HTML version deleted]]
 __
 -h...@r-project.org mailing list
 ttps://stat.ethz.ch/mailman/listinfo/r-help
 LEASE do read the posting guide http://www.R-project.org/posting-guide.html
 nd provide commented, minimal, self-contained, reproducible code.



        [[alternative HTML version deleted]]

 __
 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
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.