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) Thanks, Randy Cass [[alternative HTML version deleted]]
Hi Randy,> 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; >I admit I didn't read all the sample code you added, but this is what I *think* you're trying to accomplish. mydataframe[mydataframe$FTIStandKey == "NAH6253-003", "CoverType"] <- "XSOP-9999-C" etc.> # create a toy dataframe > mydataframe <- data.frame(A = c("var1", "var2", "var3", "var3"), B = c(10, 11, 12, 13), C = c("y1", "y3", "y8", "y3"), stringsAsFactors = FALSE) > mydataframeA B C 1 var1 10 y1 2 var2 11 y3 3 var3 12 y8 4 var3 13 y3> > # where A is "var3", add 10 to the value in the B column > mydataframe[mydataframe$A == "var3", "B"] <- mydataframe[mydataframe$A == "var3", "B"] + 10 > mydataframeA B C 1 var1 10 y1 2 var2 11 y3 3 var3 22 y8 4 var3 23 y3> > # where A is "var2", change the value in column C to "other" > mydataframe[mydataframe$A == "var2", "C"] <- "other" > mydataframeA B C 1 var1 10 y1 2 var2 11 other 3 var3 22 y8 4 var3 23 y3 There are other ways to do the same thing, of course. I find this one easy to remember. You might want to read some documentation on subsetting. Please don't repost your question right away. Most of us are busy and will answer as soon as we can. Sarah -- Sarah Goslee http://www.functionaldiversity.org