2

I have a data frame like this:

> df <- data.frame(A=c("a",NA,"b"),B=c(NA,"c",NA))
> df
     A    B
1    a <NA>
2 <NA>    c
3    b <NA>

How do I get to:

> df
  A
1 a
2 c
3 b
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Alby
  • 5,212
  • 6
  • 38
  • 50
  • The answers [here](http://stackoverflow.com/q/19253820/324364) might be helpful. – joran Apr 01 '14 at 18:36
  • ...also, please pay attention to your tags. The description for the tips-and-tricks tag says in big bold letters not to use it. – joran Apr 01 '14 at 19:05

2 Answers2

2

like this?

 df <- with(df, data.frame(AB=ifelse(is.na(A), as.character(B), as.character(A))))
> df
  AB
1  a
2  c
3  b
Jilber Urbina
  • 53,125
  • 10
  • 108
  • 134
0

There are many ways to do this. Here's one way

data.frame(A=apply(df,1,na.omit))
kdauria
  • 5,987
  • 3
  • 30
  • 50