s-news
[Top] [All Lists]

Re: Difference in data frame

To: "Tristan Lorino" <tristan.lorino@lcpc.fr>
Subject: Re: Difference in data frame
From: "Dimitris Rizopoulos" <dimitris.rizopoulos@med.kuleuven.be>
Date: Tue, 20 Jun 2006 13:00:46 +0200
Cc: <s-news@lists.biostat.wustl.edu>
References: <531310754.20060620124751@lcpc.fr>
try the following:

dat <- data.frame(ID = c(1, 1, 2, 2, 2), V2 = c(0.3, 0.4, 0.6, 0.7, 0.4), V3 = c(3, 2, 6, 9, 4))

dat$V4 <- unlist(tapply(dat$V3, dat$ID, function(x) c(x[1], diff(x))))
dat


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
    http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- From: "Tristan Lorino" <tristan.lorino@lcpc.fr>
To: <s-news@lists.biostat.wustl.edu>
Sent: Tuesday, June 20, 2006 12:47 PM
Subject: [S] Difference in data frame


Hi,

I have a data frame like that:

ID     V2     V3
1      0.3    3
1      0.4    2
2      0.6    6
2      0.7    9
2      0.4    4

I would like to have:

ID     V2     V3     V4
1      0.3    3      3
1      0.4    2      -1
2      0.6    6      6
2      0.7    9      3
2      0.4    4      -5

that means all the first data frame plus the difference between two
repeated values for V3 -- the repetitions are determined by the "ID"
variable. I tried combinations of diff and sapply functions, but
without any result...

Thank you,
Tristan Lorino


--
Laboratoire Central des Ponts et Chaussées
[Division ESAR ? Section AGR]
Route de Bouaye BP 4129
44341 Bouguenais Cedex
France
Tél 33 (0)2 40 84 56 18
Fax 33 (0)2 40 84 59 92

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm


<Prev in Thread] Current Thread [Next in Thread>