[R] Accessing the index of factor in by() function

Ista Zahn izahn at psych.rochester.edu
Tue Jul 26 06:18:10 CEST 2011


Hi Merik,
by() works most easily with data.frames. Is this what you are after?

my.plot <- function(dat) { print(dat$value);
print(dat$month[dat$id==dat$value]) }
by(dat.tmp, id, my.plot)

Best,
Ista

On Mon, Jul 25, 2011 at 9:19 PM, Merik Nanish <merik.nanish at gmail.com> wrote:
> Hello,
>
> Here are three vectors to give context to my question below:
>
> *id    <- c(1,1,1,1,1,2,2,2,3,3,3))
> month <- c(1, 1, 2, 3, 6, 2, 3, 6, 1, 3, 5)
> value <- c(10, 12, 11, 14, 16, 12, 10, 8, 14, 11, 15)*
>
> and I want to plot "value" over "month" separately for each "id". Before I
> can do that, I need to section both month and value, based on ID. I create a
> my.plot function like this (at this point, it doesn't draw any plots, it is
> just an effort to help my understand what I'm doing):
>
> *my.plot <- function(y) { print(y); print(month[id==y]) }*
>
> Now, I tried:
>
> *by(value, id, my.plot)*
>
> But of course, it didn't do what I wanted. I realized that the parameter
> passed to my.plot, is a "secion of value" per ID, and not the ID value
> itself. Question is, how can I get the value of factor ID at each level of
> by()?
>
> Please advise,
>
> Merik
>
>        [[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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list