jeudi 13 juin 2019

R if is.na() is True then perform a function not working; the condition has length > 1 and only the first element will be used

I'm trying to use an if statement that says if a value is.na, then perform a function on a different column.

I can't get it to work, and I keep getting an error:

  the condition has length > 1 and only the first element will be used

I've looked at the other questions regarding if statements, but I don't need to substitute one value for another. Instead, I need to run a function if is.na() = TRUE, and the function that I'm using (mutate_geocode) automatically makes the new columns, so I don't need to assign it to a new column. Here's what I've been trying:

library(dplyr)
library(ggmap)

Enrollment_Report2 <- if (is.na(Enrollment_Report$lon)) {
  mutate_geocode(facility_city)
}

A sample of the data looks like this:

library(dplyr)
Enrollment_Report <- tibble(facility_city = c("Atlanta", "Boston", "Tokyo"),
lon = c(NA, NA, 139.65),
lat = c(NA, NA, 35.68))

Aucun commentaire:

Enregistrer un commentaire