Skip to content
Back to formatted view

Raw Message

Message-ID: <F151C5EFCA66314D9FE66CB7199F4AAD5A3451@sad6cd1.ch.ssa.gov>
Date: 2003-07-18T20:01:49Z
From: Siddique, Amer
Subject: create a vector looping over a frame

Hello,

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))

if i try

for(i in 4:13){
sumVar<- sum(popA[,i])
sumVar2<-c(sumVar) 
}

it returns 

> sumVar2
[1] 287567

> length(sumVar2)
[1] 1

which is only the value at the last spot. i cant quite figure this simple
loop. 

Thanks,

Amer Siddique