[R] Trying get away from the "for" loop
Jason Rupert
jasonkrupert at yahoo.com
Thu Oct 15 23:27:11 CEST 2009
Argh...
I just realized the inefficiency of one of my loops so I am trying to get rid of it.
I have two data frames:
names(SubdivisionHouses)
"BuildYear", "SqrFootage", "Exterior"
names(BuildingCodes)
"Year", "Codes"
I am trying to add on the the "Codes" column to according to the "BuildYear"
Currently I am doing the following
SubdivisionHouseMod<-NULL
for(hh in 1:unique(BuildingCodes$Year))
{
SubdivisionHouses_tmp<-subset(SubdivisionHouses, SubdivisionHouses$BuildYear == BuildingCodes$Year)
BuildingCodes_tmp<-subset(BuildingCodes, BuildingCodes$BuildYear == BuildingCodes[hh, ]$BuildYear)
SubdivisionHouseMod_tmp<-cbind(SubdivisionHouses_tmp, BuildingCodes_tmp$Codes)
SubdivisionHouseMod<-rbind(SubdivisionHouseMod, SubdivisionHouseMod_tmp)
}
Doing this seems to work, but it also seems to be really inefficient.
Are there any suggestions of ways to cut this down and speed things up?
Thanks again.
More information about the R-help
mailing list