I would love to replace empty/NA values from a data table with other values that are shown in the variable of the same data table
So I have something like this:
| Country | Nationality | currentLoc |
|---|---|---|
| AR | ES | RU |
| empty | empty | ES |
| empty | AR | AR |
| EN | EN | EN |
| EN | ES | ES |
Result should be:
| Country | Nationality | currentLoc |
|---|---|---|
| AR | ES | RU |
| ES | empty | ES |
| AR | AR | EN |
| EN | EN | EN |
| EN | ES | ES |
I wrote the following R code:
df1$Country[df1$Country== "" | df1$Country==" "] <- "empty"
df1$Country <-ifelse(df1$Country == "empty", df1$Nationality, ifelse(df1$Nationality == "empty", df1$currentLoc, df1$Country))
However not all "empty" cells are replaced in the "Country" Column.
I tried already to specify the empty spaces in the df1$Country with NA, so I have done like
df1$Country[df1$Country== "" | df1$Country==" "] <- NA
df1$Country <-ifelse(is.na(df1$Country), df1$Nationality, ifelse(is.na(df1$Nationality), df1$currentLoc, df1$Country))
That is unfortunately also not working.
Could you help me out in this? I searched for similar questions here, but did not quit get the desired response.
Any help very much appriciated. :)
Cheers
Le1nO