mardi 5 mai 2015

R code for substituting values from one column to the missing values in another column

I have a data set named one with four columns: D1, D2, D3 and D4. D1 is the id. D2 has seven levels (a, b, c, d, e, f, g). D3 has missing data, which I want to fill by matching conditions from columns D2 and D4. I am selecting values from column D4 corresponding to four levels (a, c, d, e) of column D2 and then replacing the missing values of column D3 with those from D4.

D1  D2  D3  D4
1   a   .   5
2   c   12  6
3   e   .   3
4   b   .   7
5   f   .   8
6   e   .   9
7   e   11  8
8   c   .   3
9   c   52  5
10  a   .   6
11  b   4   7
12  f   .   2
13  f   .   10
14  d   .   12
15  d   .   13
16  e   .   24
17  a   1   54
18  b   2   19
19  c   5   21

I have following solution but it is not working. Any suggestion or help? Thanks.

index <- with(one, D2 %in% c('a','c','d','e'))
one$D4[index] <- one$D3[index]
one

Aucun commentaire:

Enregistrer un commentaire