Help guys, I'm trying to Impute missing values using a function, the function itself works in this respect, but fails to store the imputed values.
The following shows the function, gh_Df is the dataset; val
is the value in FacilityName variable and lat
and long
are the respective values I hope to fill.
fill_lat_long_na <- function(val, lat, long){
if(is.na(gh_Df[gh_Df$FacilityName == val,]$Latitude)){
gh_Df[gh_Df$FacilityName == val,]$Latitude <- lat
gh_Df[gh_Df$FacilityName == val,]$Longitude <- long
}
print(gh_Df[gh_Df$FacilityName == val,])
}
## Check
fill_lat_long_na("Yapesa St.Mary Clinic", 6.43011, -1.33299)
Latitude | Longitude |
---|---|
6.43011 | -1.33299 |
However if I go back and run the following outside the function it still shows the empty / NA rows.
print(gh_Df[gh_Df$FacilityName == "Yapesa St.Mary Clinic",])
Latitude | Longitude |
---|---|
NA | NA |
Is there a way to go about this and the values actually change within the dataset?
Thanks.
Usually it is not a good practice to change a dataframe from inside the function. You can return
the changed dataframe and save it outside the function.
fill_lat_long_na <- function(val, lat, long){
if(is.na(gh_Df[gh_Df$FacilityName == val,]$Latitude)){
gh_Df[gh_Df$FacilityName == val,]$Latitude <- lat
gh_Df[gh_Df$FacilityName == val,]$Longitude <- long
}
return(gh_Df)
}
gh_Df <- fill_lat_long_na("Yapesa St.Mary Clinic", 6.43011, -1.33299)
gh_Df