-1

I don't have the slightest idea of programming, but I need to solve the following problem in R.

Let's suppose I have this data:

x     y    
5     8
6     5
2     
9     8
4
0
6     6
7     3
3     2

I need to create a third column called "z" containing the data of "y" exccept for the missing values where it should have the values of "x". It would be something like this:

x     y     z
5     8     8
6     5     5
2           2
9     8     8
4           4
0           0
6     6     6
7     3     3
3     2     2
MLavoie
  • 9,671
  • 41
  • 36
  • 56

1 Answers1

0
dat <- data.frame(x=c(5,6,2,9,4,0,6,7,3), y = c(8,5,NA,8,NA,NA,6,3,2))

library(tidyverse)

dat %>%  mutate(z = ifelse(is.na(y), x, y))

#   x  y z
# 1 5  8 8
# 2 6  5 5
# 3 2 NA 2
# 4 9  8 8
# 5 4 NA 4
# 6 0 NA 0
# 7 6  6 6
# 8 7  3 3
# 9 3  2 2
Adam Quek
  • 6,973
  • 1
  • 17
  • 23