[R] Trying get away from the "for" loop
David Winsemius
dwinsemius at comcast.net
Thu Oct 15 23:46:08 CEST 2009
You do not offer a workable example, but it appears you might be doing
a merge operation.
?merge
merge(SubdivisionHouses, BuildingCodes, by.x="BuildYear", by.y="Year")
--
David
On Oct 15, 2009, at 5:27 PM, Jason Rupert wrote:
> 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.
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list