Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 正则表达式将退出名称_R - Fatal编程技术网

R 正则表达式将退出名称

R 正则表达式将退出名称,r,R,我有一张杂乱无章的地名表 place District1 / Commune2 / Village3 Region1 / District2 District3 / Commune1 / Village1 / Farm1 所以我们有不同分辨率的地名。我想提取姓氏用于地理编码目的,例如 place Village3 District2 Farm1 有人能

我有一张杂乱无章的地名表

                                  place
        District1 / Commune2 / Village3
                    Region1 / District2
District3 / Commune1 / Village1 / Farm1
所以我们有不同分辨率的地名。我想提取姓氏用于地理编码目的,例如

    place
 Village3
District2
    Farm1
有人能给我一个正则表达式,它搜索最后一个“/”并在其后只保留地名吗?非常感谢。令我困惑的是R正则表达式查询中的转义字符。

将最后一个斜杠和空格之前的所有内容替换为空字符串:

gsub(".*/ ", "", DF$place)
## [1] "Village3"  "District2" "Farm1" 
笔记 可复制形式的输入为:

Lines <- "
                                  place
        District1 / Commune2 / Village3
                    Region1 / District2
District3 / Commune1 / Village1 / Farm1"
DF <- read.table(text = Lines, header = TRUE, as.is = TRUE, sep = ";")
行将最后一个斜杠和空格
“/”
之前的所有内容替换为空字符串:

gsub(".*/ ", "", DF$place)
## [1] "Village3"  "District2" "Farm1" 
笔记 可复制形式的输入为:

Lines <- "
                                  place
        District1 / Commune2 / Village3
                    Region1 / District2
District3 / Commune1 / Village1 / Farm1"
DF <- read.table(text = Lines, header = TRUE, as.is = TRUE, sep = ";")
行这也适用于:

trimws(basename(df$place))
这也适用于:

trimws(basename(df$place))

有些农场有相同的名字。谷歌地图API需要更多信息来提供更准确的地理位置。可能需要添加村庄名称和农场名称。必须考虑如何扩展gsub表达式以使用这两个名称。有些农场有相同的名称。谷歌地图API需要更多信息来提供更准确的地理位置。可能需要添加村庄名称和农场名称。必须考虑如何扩展gsub表达式以使用这两个名称。