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,753
  • 72
  • 57
  • 129
Alby
  • 5,522
  • 7
  • 41
  • 51
  • 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
  • 58,147
  • 10
  • 114
  • 138
0

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

data.frame(A=apply(df,1,na.omit))
kdauria
  • 6,300
  • 4
  • 34
  • 53