I have a dataframe with incorrect values for two variables (Lat and Lon). The incorrect values in the dataframe are listed as 999.00, and the correct values should be 42.68 and -72.47, respectively.
I would like an easy way to replace these values using dplyr, but my attempts (see below) have been unsuccessful (errors provided below).
df$Lat2 <- recode(df$Lat, "999.00"="42.68", .default=x)
Error in lapply(x, f) : object 'x' not found
df <- df %>%
mutate(Lat2 = if_else(Lat == 999.00, 42.68, NULL, NULL))
Error in mutate_impl(.data, dots) : Evaluation error: unused argument (recvLat = 999).
df <- df %>%
mutate(Lat2 = ifelse(Lat == 999.00, 42.68, NULL))
Error in mutate_impl(.data, dots) : Evaluation error: replacement has length zero. In addition: Warning message: In rep(no, length.out = length(ans)) : 'x' is NULL so the result will be NULL
df <- df %>%
mutate(Lat2 = case_when(Lat == 999.00 ~ 42.68, TRUE ~ NULL))
Error in mutate_impl(.data, dots) : Evaluation error: subscript out of bounds.
For the latter three attempts, I get the same error if the number are in quotes (i.e. "999.00" and "42.68")
ifelse
statement should have the actual column as the alternative, notNULL
, i.e.ifelse(..,..., df$Lat)
– Sotosdf$Lat <- df$Lat %>% gsub("999.00", "42.68")
? – huandf$Lat
is a numeric variable, not a string – Sotos