Thank you again for the suggestions to use merge:
merge(SubdivisionHouses, BuildingCodes, by.x="BuildYear",
by.y="Year")
It appears to work perfectly for the example shown below:
BuildYear<-c(1980, 1985, 1975, 1990, 1980)
SqrFootage<-c(1500, 1650, 1500, 2000, 1450)
Exterior<-c("Brick", "Stone", "Siding",
"Brick", "Siding")
SubdivisionHouses<-data.frame(BuildYear, SqrFootage, Exterior)
Year<-c(1980, 1985, 1975, 1990, 1977, 1986)
Codes<-c("G1", "G5", "G6", "B5",
"B6", "B7")
BuildingCodes<-data.frame(Year, Codes)
SubdivisionHouseMod<-NULL
for(hh in 1:unique(BuildingCodes$Year))
{
SubdivisionHouses_tmp<-subset(SubdivisionHouses,
SubdivisionHouses$BuildYear == BuildingCodes[hh, ]$Year)
if(length(SubdivisionHouses_tmp$BuildYear) >0)
{
BuildingCodes_tmp<-subset(BuildingCodes, BuildingCodes$Year ==
BuildingCodes[hh, ]$Year)
SubdivisionHouseMod_tmp<-cbind(SubdivisionHouses_tmp,
BuildingCodes_tmp$Codes)
SubdivisionHouseMod<-rbind(SubdivisionHouseMod,
SubdivisionHouseMod_tmp)
}
}
Thank you again.
--- On Thu, 10/15/09, David Winsemius <dwinsemius at comcast.net> wrote:
> From: David Winsemius <dwinsemius at comcast.net>
> Subject: Re: [R] Trying get away from the "for" loop
> To: "Jason Rupert" <jasonkrupert at yahoo.com>
> Cc: R-help at r-project.org
> Date: Thursday, October 15, 2009, 4:46 PM
> 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
>
>