[R] Using a 'for' loop : there should be a better way in R
John Kane
jrkrideau at yahoo.ca
Thu Aug 24 23:30:32 CEST 2006
I need to apply a yearly inflation factor to some
wages and supply some simple sums by work category. I
have gone at it with a brute force "for" loop approach
which seems okay as it is a small dataset. It looks
a bit inelegant and given all the warnings in the
Intro to R, etc, about using loops I wondered if
anyone could suggest something a bit simpler or more
efficent?
Example:
cat1 <- c( 1,1,6,1,1,5)
cat2 <- c( 1,2,3,4,5,6)
cat3 <- c( 5,4,6,7,8,8)
cat4 <- c( 1,2,1,2,1,2)
years <- c( 'year1', 'year2', 'year3', 'year3',
'year1', 'year1')
id <- c('a','a','b','c','c','a')
df1 <- data.frame(id,years,cat1,cat2, cat3, cat4)
nn <- levels(df1$id) # levels for outer loop
hh <- levels(df1$years) # levels for inter loop
mp <- c(1, 5, 10) # inflation factor
tt <- data.frame(matrix(NA, length(nn), 2))
names(tt) <- c("s1","s2")
rownames(tt) <- nn
for (i in 1:length(nn)){
scat <- data.frame(matrix(NA, length(hh),2))
dd1 <- subset(df1, id==nn[i])
for (j in 1:length(hh)){
dd2 <- subset(dd1, dd1$years==hh[j])
s1 <- sum(dd2$cat1,dd2$cat2, na.rm=T)
s2 <- sum(dd2$cat3,dd2$cat4,na.rm=T)
scat[j,] <- c(s1,s2) *mp[j] # multiply by the
inflation factor
}
crush <- apply(scat, 2, sum)
tt[i,] <- crush
}
tt
More information about the R-help
mailing list