1

How do I combine two columns and replace missing values in R?

Let's say I have a data.frame:

D = data.frame(a = c(1,NA,3), b = c(1,2,NA))

Looks like this

   a  b
1  1  1
2 NA  2
3  3 NA

How would I get it to look like this:

  c
1 1
2 2
3 3
John Waller
  • 2,257
  • 4
  • 21
  • 26
  • 3
    See e.g [**here**](http://stackoverflow.com/questions/16288031/combine-only-values-across-two-columns-in-r) or [**here**](http://stackoverflow.com/questions/22106132/combine-merge-columns-while-avoiding-na). – Henrik Apr 09 '14 at 13:29

2 Answers2

2

Assuming the two columns will either have the same values or one of them will be NA

D = data.frame(a = c(1,NA,3), b = c(1,2,NA))
D[['c']] <- rowMeans(D, na.rm = TRUE)
D

   a  b c
1  1  1 1
2 NA  2 2
3  3 NA 3
Anto
  • 1,189
  • 10
  • 16
  • 1
    Just curious: why use `D[['c']]` instead of `D$c`?? – jlhoward Apr 09 '14 at 14:08
  • It helps me pass variables. So, if i have a column whose name changes often, I can simply store that in a variable and use this variables, when I want to use loops for in functions. This way I treat a data.frame as if it is list, which is what it is. So, I can do D['c'] as well. There is more to it than I know of and I may not be totally right. – Anto Apr 09 '14 at 18:24
2

If Column a is the master column and you want to copy values in b that are NA in a then:

D = data.frame(a = c(1,NA,3), b = c(1,2,NA))
D2=data.frame(c = D$a)
D2$c[is.na(D2$c)] <- D$b[is.na(D$a)]
D2
  c
1 1
2 2
3 3
imran
  • 1,560
  • 10
  • 8