"If else" changing categorical data to "1", not modifying all data

Hi everyone, I'm really new to R so I apologize if the answer to this is obvious.
I was trying to change instances of "NA" from the "Country" column of my data to the country that matches the city that the data is located in. It seemed to work, however, it changed all other data under "Country" to "1", and doesn't seem to have replaced the other "NA" when I tried to do the same for other missing country data that had a value for "City". Here's the code I used:

Apartify_GroupA_new <- Apartify_GroupA_new %>% 
  mutate(Country = ifelse(is.na(Country) & Apartify_GroupA_new$City == "Beijing", 
                                      "China",
       Country))
diagnose(Apartify_GroupA_new)

Here is a screenshot of what happened to my data:


Here is another example where the "if else" function seems to have partially worked:

Apartify_GroupA_new <- Apartify_GroupA_new %>% 
  mutate(Bedrooms = ifelse(is.na(Bedrooms) & Apartify_GroupA_new$Beds == "1", 
                                      "1",
       Bedrooms),
       ifelse(is.na(Bedrooms) & Apartify_GroupA_new$Beds == "2",
              "2",
              Bedrooms),
       ifelse(is.na(Bedrooms) & Apartify_GroupA_new$Beds == "4",
              "4",
              Bedrooms))
diagnose(Apartify_GroupA_new)

I really appreciate any help!

This might work for you , just change Country as as.character(Country).

Apartify_GroupA_new <- Apartify_GroupA_new %>%
mutate(Country = ifelse(is.na(Country) & Apartify_GroupA_new$City == "Beijing",
"China",
as.character( Country)))

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.