As I suspected, S-NEWS experts came through in no time at all.
As summary, the question that I posed is "How can one replace values in
specified columns of a data frame -- for instance, replace the "5s" in
columns 1 and 3 with NA while leaving column 2 alone?"
I had been thinking in terms of the "ifelse" command without an else and was
way off target.
The most succinct answer was provided by Leonid Gibiansky and Bert Gunter
and involved a clever use of subscripting. Here is an example involving
replacing 5's with NA in columns 1 and 3, but leaving column 2 alone.
> TEMP<-data.frame(VAR1=rep(seq(1:5),2),VAR2=seq(1:10),VAR3=rep(seq(1:5),2))
> TEMP
VAR1 VAR2 VAR3
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 1 6 1
7 2 7 2
8 3 8 3
9 4 9 4
10 5 10 5
> TEMP[,c(1,3)][TEMP[,c(1,3)]==5]<-NA #THE PROVIDED ANSWER
> TEMP
VAR1 VAR2 VAR3
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 NA 5 NA
6 1 6 1
7 2 7 2
8 3 8 3
9 4 9 4
10 NA 10 NA
My only addition to this is that it needs to be a little more complicated if
your dataframe has NA values. For instance, if I want to replace the 1's
with NAs in the TEMP data frame (that now contains NAs from my last
manipulation) the command needs to be:
> TEMP[,c(1,3)][TEMP[,c(1,3)]==1&is.na(TEMP[,c(1,3)])==F]<-NA
> TEMP
VAR1 VAR2 VAR3
1 NA 1 NA
2 2 2 2
3 3 3 3
4 4 4 4
5 NA 5 NA
6 NA 6 NA
7 2 7 2
8 3 8 3
9 4 9 4
10 NA 10 NA
Thanks for the help. This command will save me a lot of time dealing with
data frames where multiple columns that need recoding.
Paul
|