Randy Cass
2010-Apr-26 17:15 UTC
[R] Help with replacement of certain values in dataset with SAS code equivalent
I am new to R and have tried for a good while to figure out how to code this in R. The dataset below: FTIStandKey State County FTITract CoverType Ver_CT V_Origin V_SpGrp NAH6005-001 Texas Jasper NAH6005 PPLB-2000-U PPLB-2000-U P P NAH6005-002 Texas Jasper NAH6005 NHHX-1950-O NHHX-1950-I N H NAH6253-001 Texas Tyler NAH6253 PPLB-2001-U PPLB-2001-U P P NAH6253-002 Texas Tyler NAH6253 PPLB-1993-U PPLB-1993-U P P NAH6253-003 Texas Tyler NAH6253 PPLB-2003-U PPLB-2003-U P P NAH6253-005 Texas Tyler NAH6253 XSOP-9999-C XSOP-9999-C X S NAH6253-008 Texas Tyler NAH6253 NPLB-1975-O NPLB-1975-O N P The SAS code for what I want to do is: /* if FTIStandKey=" NAH6253-003" then do; CoverType=" XSOP-9999-C "; V_SpGrp="T"; V_Origin="T"; end; What I want to do is to be able to select certain FTIStandKey's and change values in the dataset. For example, I would like to select by FTIStandKey=="NAH6253-003", and change the entries for CoverType=" XSOP-9999-C " and change the V_SpGrp=="T", and also change V_Origin=="T". I only want to change the record for the one FTIStandKey. All the other records need to remain the exact same. I have spent a good bit of time trying to figure this out but with no success. Any help would be greatly appreciated. The only thing I could find even close to what I wanted to do is listed below: # R Program for Multiple Conditional Transformations. # Read the file into a data frame and print it. load(file="c:\\mydata.Rdata") print(mydata) # Use column bind to add two new columns to mydata. # Not necessary for this example, but handy to know. mydata <- cbind(mydata, score1 = 0, score2 = 0) attach(mydata) mydata$guys <- gender=="m" #Creates a logical vector for males. mydata$gals <- gender=="f" #Creates another for females. print(mydata) # Applies basic math to the group selected by the logic. mydata$score1[gals]<- 2*q1[gals] + q2[gals] mydata$score2[gals]<- 3*q1[gals] + q2[gals] mydata$score1[guys]<-20*q1[guys] + q2[guys] mydata$score2[guys]<-30*q1[guys] + q2[guys] print(mydata) # This step uses NULL to delete the guys & gals vars. mydata$gals <- mydata$guys <- NULL print(mydata) This is how I modified the code from above to try to do what I wanted: StandFTI <- cbind(StandFTI, score1 = NA, score2 = NA) attach(StandFTI) StandFTI$Stand <- FTIStandKey=="NAH6005-001" #Creates a logical vector for males. StandFTI$score1[Stand]<- "TEST" print(StandFTI) But I get the warning message: In `[<-.factor`(`*tmp*`, guys, value = "TEST") : invalid factor level, NAs generated Any help will be greatly appreciated! Thanks, Randy Cass [[alternative HTML version deleted]]
Erik Iverson
2010-Apr-26 17:54 UTC
[R] Help with replacement of certain values in dataset with SAS code equivalent
> What I want to do is to be able to select certain FTIStandKey's and change > values in the dataset. For example, I would like to select by > FTIStandKey=="NAH6253-003", and change the entries for CoverType=" > XSOP-9999-C " and change the V_SpGrp=="T", and also change V_Origin=="T". I > only want to change the record for the one FTIStandKey. All the other > records need to remain the exact same. I have spent a good bit of time > trying to figure this out but with no success. Any help would be greatly > appreciated.Do not know if the following will work, and it is not tested since you don't give a reproducible example, see ?dput and the Posting Guide for this list. There are several ways of doing what you want, some of which are possibly more transparent than this: mydata[mydata$FTIStandKey == "NAH6253-003", c("CoverType", "V_SpGrp", "V_Origin")] <- list(" XSOP-9999-C ", "T", "T") This relies on the fact that if your variables are factors, that they include the replacement levels already (including the fact that you have spaces on either side of the string for CoverType... You might also see ?ifelse for another method of dealing with stuff like this.