[R] (no subject)

arun smartpink111 at yahoo.com
Tue Apr 9 07:15:03 CEST 2013


You can also do 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))
 t2<-t1[t1$var=="norm",]
 t3<-t1[t1$var!="norm",]
df1<-merge(t2,t3,by=c("id","dt"))
 df1$Norm<-df1$value.y/df1$value.x
 df2<-df1[,c(1:2,5,7)]
colnames(df2)[3]<- "var1"
 head(df2)
#  id dt var1        Norm
#1  1  1 num1  1.42893951
#2  1  1 num2  0.04829956
#3  1  2 num1  1.21120856
#4  1  2 num2 -0.43157114
#5  1  3 num1 -0.74132293
#6  1  3 num2 -1.99100489


#or to preserve the order

library(plyr)
dfNew<- join(t2,t3,by=c("id","dt"))
 dfNew$Norm<- dfNew[,6]/dfNew[,4]
dfNew<- dfNew[,c(1,2,5,7)]
head(dfNew)
#  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


identical(res,dfNew)
#[1] TRUE
A.K.




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: neal subscribe <nealsubscribe at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, April 9, 2013 12:59 AM
Subject: Re: [R] (no subject)

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.



More information about the R-help mailing list