Kode:

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
}

Uji:

GeoSeparate(df3, DeathCityGeo)

Kesalahan:

Harus mengekstrak kolom dengan satu subskrip. x Subskripvarhas the wrong typequosure/formula. Harus berupa angka atau karakter.

Fungsi saya memisahkan kolom yang memiliki format: "Norwalk, CT\n(41.11805, -73.412906)" sehingga hanya tersisa garis lintang dan garis bujur dan keduanya berada dalam dua kolom terpisah. Ini berfungsi untuk sementara waktu, tetapi sekarang saya mendapatkan pesan kesalahan yang dijelaskan di atas. Mungkin karena saya memperbarui perpustakaan saya, tetapi saya tidak yakin. Bantuan apa pun akan luar biasa! Terima kasih.

1
ajg 3 Juni 2020, 00:03

1 menjawab

Jawaban Terbaik

Kita perlu mengevaluasi (!!)

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
    }

Atau opsi lain adalah 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
        }
1
akrun 2 Juni 2020, 21:05