I have a question. I have three "Nationality" variables in my dataset (as three separate columns):
Now I want to create a fourth variable "Nationality", which is based on "Nationality_difference" (focal variable), but has the values of "Nationality_birth" (substitute variable) if "Nationality_difference" is NA.
I tried the following code:
data$Nationality <- data$Nationality_difference
data$Nationality[is.na(data$Nationality_difference)] <- data$Nationality_birth
I get the following error:
Error in data$Nationality[is.na(data$Nationality_difference)] <- data$Nationality_birth :
replacement has length zero
What am I missing?
Thanks a lot in advance!
You can try:
data$Nationality <-
ifelse(!is.na(data$Nationality_difference),
data$Nationality_difference,
data$Nationality_birth)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.