Search code examples
rerror-handlingdatasetgeospatialdata-cleaning

Error in R: The subscript var has the wrong type quosure/formula. It must be numeric or character


Code:

GeoSeparate <- function(Dataset, GeoColumn) {
  GeoColumn <- enquo(GeoColumn) 
  Dataset %>% 
    separate(GeoColumn, into = c("Section1", "Section2"), sep = "\\(")%>%
    separate(Section1, into = c("Section3", "Section4"), sep = ",")%>%
    separate(Section2, into = c("GeoColumn", "Section5"), sep = "\\)")%>%
    separate(GeoColumn, into = c("GeoColumnLat", "GeoColumnLon"), sep = ",")%>%
    select(-Section3, -Section4, -Section5) #remove sections we don't need
}

Test:

GeoSeparate(df3, DeathCityGeo)

Error:

Must extract column with a single subscript. x The subscriptvarhas the wrong typequosure/formula. ℹ It must be numeric or character.

My function separates a column that has the format: "Norwalk, CT\n(41.11805, -73.412906)" so that the latitude and longitude are all that remain and they are in two separate columns. It worked for a while, but now I get the error message described above. It may be because I updated my libraries but I'm not sure. Any help would be amazing! Thank you.


Solution

  • We need to evaluate (!!)

    GeoSeparate <- function(Dataset, GeoColumn) {
        GeoColumn <- enquo(GeoColumn) 
        Dataset %>% 
            separate(!!GeoColumn, into = c("Section1", "Section2"), sep = "\\(")%>%
            separate(Section1, into = c("Section3", "Section4"), sep = ",")%>%
            separate(Section2, into = c("GeoColumn", "Section5"), sep = "\\)")%>%
            separate(!!GeoColumn, into = c("GeoColumnLat", "GeoColumnLon"), sep = ",")%>%
            select(-Section3, -Section4, -Section5) #remove sections we don't need
        }
    

    Or another option is curly-curly ({{}})

    GeoSeparate <- function(Dataset, GeoColumn) {
    
            Dataset %>% 
                separate({{GeoColumn}}, into = c("Section1", "Section2"), sep = "\\(")%>%
                separate(Section1, into = c("Section3", "Section4"), sep = ",")%>%
                separate(Section2, into = c("GeoColumn", "Section5"), sep = "\\)")%>%
                separate({{GeoColumn}}, into = c("GeoColumnLat", "GeoColumnLon"), sep = ",")%>%
                select(-Section3, -Section4, -Section5) #remove sections we don't need
            }