R中的错误。下标var的类型错误,必须是数字或字符。它必须是数字或字符

问题描述 投票:0回答:1

编码:

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
}

测试:

GeoSeparate(df3, DeathCityGeo)

错误:

Must extract column with a single subscript. x The subscriptvarhas the wrong type曝光公式. ℹ It must be numeric or character.

我的函数分离了一列,格式是: "Norwalk, CT\n(41.11805, -73.412906)",这样就只剩下经度和纬度了,它们分别在两列中。它工作了一段时间,但现在我得到了上述的错误信息。这可能是因为我更新了我的库,但我不确定。任何帮助将是惊人的! 谢谢你的帮助。

r error-handling dataset geospatial data-cleaning
1个回答
1
投票

我们需要评估(!!)

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
    }

或者另一种选择是 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
        }
© www.soinside.com 2019 - 2024. All rights reserved.