It would be easier to answer if a portion of the input were shown in the question using dput(rf) or dput(head(rf)) but lets assume it looks like this and that our objective is to display a table of Name vs. DNAME counts where DNAME consists of manufactured short names -- is that right?
DF <- data.frame(ID = c(10, 20, 10), Name = c("A blah", "B blah", "A blah")) # Then here are several alternatives: # 1. using assignment as in original question DF1 <- DF DF1$DNAME[DF1$ID == 10] <- "A" DF1$DNAME[DF1$ID == 20] <- "B" with(DF1, table(Name, DNAME)) # 2. using ifelse DF2 <- DF DF$NAME <- ifelse(DF2$ID == 10, "A", "B") # 3. using sub assuming first word is unique DF3 <- transform(DF, DNAME = sub(" .*", "", Name)) with(DF3, table(Name, DNAME)) # 4. using abbreviate although abbreviated names do not always look nice DF4 <- transform(DF, DNAME = abbreviate(Name)) with(DF4, table(Name, DNAME)) # 5. subscripting by name shortNames <- c("A blah" = "A", "B blah" = "B") DF5 <- transform(DF, DNAME = shortNames[Name]) with(DF5, table(Name, DNAME)) On Tue, Dec 8, 2009 at 7:45 PM, Arthur Burke <art.bu...@educationnorthwest.org> wrote: > I am trying to use the value of an ID variable in an if statement and > not getting the results I expected. > > # ID values for two school districts >> with(rf, tapply(DistrictID, DistrictName, min) ) > > Aberdeen School Dist. # 58 Buhl Joint School District > 59340 53409 > > > This creates DNAME as I expected ... > > rf$DNAME[rf$DistrictID==59340] <- 'Aberdeen' > rf$DNAME[rf$DistrictID==53409] <- 'Buhl' > > >> with(rf, table(DistrictName, DNAME) ) > DNAME > DistrictName Aberdeen Buhl > Aberdeen School Dist. # 58 242 0 > Buhl Joint School District 0 428 > > But these if statements ... > > if(rf$DistrictID == 59340) {rf$D.NAME <- 'Aberdeen'} > if(rf$DistrictID == 53409) {rf$D.NAME <- 'Buhl'} > > > Lead to this ... > > with(rf, table(DistrictName, D.NAME) ) > > D.NAME > DistrictName Aberdeen > Aberdeen School Dist. # 58 242 > Buhl Joint School District 428 > > > What am I doing wrong in the if statement? > > Thanks! > Art > > ************************* > > Art Burke > > Associate, Evaluation Program > > Education Northwest > > 101 SW Main St, Ste 500 > > Portland, OR 97204 > > Phone: 503.275.9592 > > art.bu...@educationnorthwest.org > <mailto:art.bu...@educationnorthwest.org> > > http://educationnorthwest.org <http://educationnorthwest.org/> > > We have recently changed our name to "Education Northwest" from > "Northwest Regional Educational Laboratory." Please note the new e-mail > and Web addresses in the signature above. You may continue to find us on > the Web at http://www.nwrel.org <http://www.nwrel.org/> for the > immediate future as well. > > ************************ > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.