[R] help with code

Sarah Goslee sarah.goslee at gmail.com
Mon Apr 26 20:02:34 CEST 2010


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)
> mydataframe
     A  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
> mydataframe
     A  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"
> mydataframe
     A  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



More information about the R-help mailing list