Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.SOL.4.44.0307181808290.7357-100000@rygar.gpcc.itd.umich.edu>
Date: 2003-07-18T22:15:17Z
From: Thomas W Blackwell
Subject: create a vector looping over a frame
In-Reply-To: <F151C5EFCA66314D9FE66CB7199F4AAD5A3451@sad6cd1.ch.ssa.gov>

On Fri, 18 Jul 2003, Siddique, Amer wrote:

> I have a data.frame
>
> > names(popA)
>  [1] "Year"   "Series" "Age"    "WM"     "WF"     "HM"     "HF"     "BM"
>  [9] "BF"     "IM"     "IF"     "AM"     "AF"     "Yr"
>
> how do i loop over a subset of variables in this frame to create a vector of
> length equal to the number of variables in the subset such that the vector's
> ith element is the result of an aggregate fncn applied to the ith variable?
>
> eg,
> sumvar<-c(sum(WM),...,sum(AF))

sumvar <- sapply(popA[ ,4:13], sum)

You can use any function, even a user-written one, in place of sum().
When you see the help for  sapply(), remember that a data.frame is
also a list ("inherits" from list).

-  tom blackwell  -  u michigan medical school  -  ann arbor  -