Skip to content

Dismiss previous email

2 messages · Judith Flores, jim holtman

#
Sorry about that, it was sent by accident.

I have a data frame that looks something like this:

   id day    k
  56  -1  566
  63  -1  680
  73  -1  773
  56   2  298
  63   2  273
  
    Of course, it is a very simplified version of the
real data frame I am working on. I need to add another
column that would represent a percent change in k from
day -1, by id. I put only two ids at day 2 to
emphasize the fact that after day -1 some subjects
won't be on the data frame any more.

I tried something like this:

pck<-by(dat,dat[,c("id","day")], function(x) {
      pc<-((x$k-x$k[x$day==-1])/x$k[x$day==-1])*100
      })
but it didn't work. 

Then I tried:

for(i in dat$id) {

    for(s in dat$day) {
        pc<-((dat$k[dat$id==i &
dat$day==s]-dat$k[dat$id==i &
dat$day==-1])/dat$k[dat$id==i & dat$day==-1])*100
}
}
without success.

I am sure it is very simple to do, but I would
appreciate any hints.

Thank you,

Judith



 


      ____________________________________________________________________________________
Be a better pen pal.
#
Does this do it for you?
+  56  -1  566
+  63  -1  680
+  73  -1  773
+  56   2  298
+  63   2  273"), header=TRUE)
+     (.data - .data[1]) / .data[1] * 100
+ })
id day   k   percent
1 56  -1 566   0.00000
2 63  -1 680   0.00000
3 73  -1 773   0.00000
4 56   2 298 -47.34982
5 63   2 273 -59.85294

        
On Nov 30, 2007 8:44 PM, Judith Flores <juryef at yahoo.com> wrote: