[R] Dismiss previous email

jim holtman jholtman at gmail.com
Sat Dec 1 05:31:34 CET 2007


Does this do it for you?

> x <- read.table(textConnection(" id day    k
+  56  -1  566
+  63  -1  680
+  73  -1  773
+  56   2  298
+  63   2  273"), header=TRUE)
> x$percent <- ave(x$k, list(x$id), FUN=function(.data){
+     (.data - .data[1]) / .data[1] * 100
+ })
>
> x
  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:
> 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.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list