[R] data normalization
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Wed Jun 20 10:17:22 CEST 2012
assuming that the entries for each subject are ordered with respect to
TIME and each subject has a measurement at TIME = 0, then you could use
the following:
Data <- read.table(textConnection("ID TIME DV
1 0 0.880146038
1 1 0.88669051
1 3 0.610784702
1 5 0.756046666
2 0 0.456263368
2 1 0.369991537
2 3 0.508798346
2 5 0.441037014
3 0 0.854905349
3 1 0.960457553
3 3 0.609434409
3 5 0.655006334"), header = TRUE)
closeAllConnections()
Data$DVn <- with(Data, ave(DV, ID, FUN = function (x) x/x[1]))
Data
If either of the above assumptions doesn't hold, you'll have to tweak it.
I hope it helps.
Best,
Dimitris
On 6/20/2012 2:08 AM, york8866 wrote:
> I have a dataframe such like the following:
>
> ID TIME DV
> 1 0 0.880146038
> 1 1 0.88669051
> 1 3 0.610784702
> 1 5 0.756046666
> 2 0 0.456263368
> 2 1 0.369991537
> 2 3 0.508798346
> 2 5 0.441037014
> 3 0 0.854905349
> 3 1 0.960457553
> 3 3 0.609434409
> 3 5 0.655006334
> . . .
> . . .
>
>
> I would like to generate another column with the normalized values of DV.
> for each ID, normalize to the value at TIME 0.
> I was able to use 2 loops to do the normalization, however, as 2 loops took
> a long time for the calculation, I don't know whether there's a better way
> to do it.
> I have the following code with only 1 loop, but it did not work. Can anyone
> help with this? Thanks,
>
> IDS <- unique(data.frame$ID)
> for(WhichID in IDS)
> {
> subset <- data.frame[,"ID"] == WhichID
> DVREAL <- data.frame[subset,"DV"]
> DVNORM[WhichID] <- DVREAL/DVREAL[1]
> }
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/data-normalization-tp4633911.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
More information about the R-help
mailing list