R: Deleting Columns When Trying To Replace

73 Views Asked by At

I'm trying to replace NA values in a column in a data frame with the value from another column in the same row. Instead of replacing the values the entire column seems to be deleted.

fDF is a data frame where some values are NA. When column 1 has an NA value I want to replace it with the value in column 2.

fDF[columns[1]] = if(is.na(fDF[columns[1]]) == TRUE & 
                     is.na(fDF[columns[2]]) == FALSE) fDF[columns[2]]

I'm not sure what I'm doing wrong here.

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

You can adjust following code to your data:

> ddf
   xx yy    zz
1   1 10 11.88
2   2  9    NA
3   3 11 12.20
4   4  9 12.48
5   5  7    NA
6   6  6 13.28
7   7  9 13.80
8   8  8 14.40
9   9  5    NA
10 10  4 15.84
11 11  6 16.68
12 12  6 17.60
13 13  5 18.60
14 14  4 19.68
15 15  6    NA
16 16  8 22.08
17 17  4 23.40
18 18  6 24.80
19 19  8    NA
20 20 11 27.84
21 21  8 29.48
22 22 10 31.20
23 23  9 33.00
> 
> 
> idx = is.na(ddf$zz)
> idx
 [1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE
[22] FALSE FALSE
> 
> ddf$zz[idx]=ddf$yy[idx]
> 
> ddf
   xx yy    zz
1   1 10 11.88
2   2  9  9.00
3   3 11 12.20
4   4  9 12.48
5   5  7  7.00
6   6  6 13.28
7   7  9 13.80
8   8  8 14.40
9   9  5  5.00
10 10  4 15.84
11 11  6 16.68
12 12  6 17.60
13 13  5 18.60
14 14  4 19.68
15 15  6  6.00
16 16  8 22.08
17 17  4 23.40
18 18  6 24.80
19 19  8  8.00
20 20 11 27.84
21 21  8 29.48
22 22 10 31.20
23 23  9 33.00
> 
1
On

You want an ifelse() expression:

fDF[columns[1]] <- ifelse(is.na(fDF[columns[1]]), fDF[columns[2]], fDF[columns[1]])

not trying to assign the result of an if statement to a vector, which doesn't make any sense.

[EDIT only for David Arenburg: if that wasn't already explicit enough, in R if statements are not vectorized, hence can only handle scalar expressions, hence they're not what the OP needed. I had already tagged the question 'vectorization' yesterday and the OP is free to go read about vectorization in R in any of the thousands of good writeups and tutorials out there.]