# [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()?
>
>
> Merik
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Ista Zahn