[R] Help with one of "those" apply functions

Phil Spector spector at stat.berkeley.edu
Wed Feb 2 22:54:05 CET 2011


Robin -
    Does

tapply(value,id,sum)

do what you want?
 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Wed, 2 Feb 2011, Robin Jeffries wrote:

> Hello there,
>
> I'm still struggling with the *apply commands. I have 5 people with id's
> from 10 to 14. I have varying amounts (nrep) of repeated outcome (value)
> measured on them.
>
> nrep <- 1:5
> id    <- rep(c("p1", "p2", "p3", "p4", "p5"), nrep)
> value <- rnorm(length(id))
>
> I want to create a new vector that contains the sum of the values per
> person.
>
> subject.value[1] <- value[1]    # 1 measurement
> subject.value[2] <- sum(value[2:3]) # the next 2 measurements
> ...
> subject.value[5] <- sum(value[11:15])  # the next 5 measurements
>
>
> I'd imagine it'll be some sort of *apply(value, nrep, sum) but I can't seem
> to land on the right format.
>
> Can someone give me a heads up as to what the correct syntax and function
> is?
>
> Danke,
>
> Robin Jeffries
> MS, DrPH Candidate
> Department of Biostatistics
> UCLA
> 530-624-0428
>
> 	[[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