I’m trying to work with conditional but don’t find an easy way to do it. I have a dataset with missing value in column As, I want to create a new column C that takes the original values in A for all the rows without missing, and for row with missing value take the value from another column (column B). All columns are character variables.
| A | B |
|---|---|
| 13 A 1 | 15 A 2 |
| 15 A 2 | 15 A 2 |
| NA | 15 A 8 |
| 10 B 3 | 15 A 2 |
| NA | 15 A 5 |
What i want is:
| A | B | C |
|---|---|---|
| 13 A 1 | 15 A 2 | 13 A 1 |
| 15 A 2 | 15 A 2 | 15 A 2 |
| NA | 15 A 8 | 15 A 8 |
| 10 B 3 | 15 A 2 | 10 B 3 |
| NA | 15 A 5 | 15 A 5 |
I tried with a loop but the result is not satisfactory,
for(i in 1:length(df$A)) {
if(is.na(df$A[i])) {
df$C <- df$B
}
else {
df$C<- df$A
}
}
If anyone can help me, Thanks in advance