Hi,
Try this:
set.seed(28)
?t1<-data.frame(id=rep(1:3,rep(3,3)),dt=rep(1:3,rep(9,3)),var=c('num1','num2','norm'),value=rnorm(27))
?head(t1)
#? id dt? var?????? value
#1? 1? 1 num1 -1.90215722
#2? 1? 1 num2 -0.06429479
#3? 1? 1 norm -1.33116707
#4? 2? 1 num1 -1.81999167
#5? 2? 1 num2? 0.16266969
#6? 2? 1 norm? 0.53139634
?res<-do.call(rbind,lapply(split(t1,list(t1$id,t1$dt)),function(x)
{x$Norm<-x$value/tail(x$value,1);head(x[,-4],-1)}))
?row.names(res)<-1:nrow(res)
?head(res)
#? id dt? var??????? Norm
#1? 1? 1 num1? 1.42893951
#2? 1? 1 num2? 0.04829956
#3? 2? 1 num1 -3.42492323
#4? 2? 1 num2? 0.30611744
#5? 3? 1 num1 -0.94940980
#6? 3? 1 num2 -0.06622304
A.K.
----- Original Message -----
From: neal subscribe <nealsubscribe at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Monday, April 8, 2013 10:49 PM
Subject: [R] (no subject)
Hi
I would like to normalize my data by one of the variables in long format.
My data is like this:
>
t1<-data.frame(id=rep(1:3,rep(3,3)),dt=rep(1:3,rep(9,3)),var=c('num1','num2','norm'),value=rnorm(27))> t1
? id dt? var? ? ? value
1? 1? 1 num1 -1.83276256
2? 1? 1 num2? 1.57034303
3? 1? 1 norm? 0.60008563
4? 2? 1 num1 -0.96893477
5? 2? 1 num2? 0.30423346
6? 2? 1 norm -0.07044640
7? 3? 1 num1 -1.30558219
...
in this case there are 3 ids and 3 dates (dt).? on each date, each id has a
normalization factor given by the variable norm.? i would like to divide all
the variables for each id by the normalization on that date.? (i don't care
if the normalization is divided by itself since it should always be end up
1, might be a useful check.) so i would like to end up with a data frame
like
1? 1? num1? -1.83/.6
1? 1? num2? -1.57/.6
2? 1? num1? -.97/(-.07)
2? 1? num2? .3/(-.07)
etc.? The actual data frame is quite long with about many id's and dates.
Thanks!
Neal
??? [[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.