模式匹配与替换/ R中的数据清理

时间:2018-07-26 11:43:18

标签: r pattern-matching geospatial data-cleaning

我要绘制地理空间数据,因此需要坐标。我提供的信息非常混乱,我需要一个好的系统,将多种格式的坐标矢量转换为如下所示的一种有用格式:

输入:

- lat <- c("41º12'23.33''", "40º39'15.6'", "41 10 589", "38 31 10.6",
   "38.720647") 
 - lon <- c("8º19'40.66''", "7º52'31.95'", "8 37 832", "8    54 17.0",
   "-9.22522")

输出:

- lat <- c(41.122333, 40.39156, 41.10589, 38.31106, 38.720647)
 - lon <- c(8.194066, 7.523195, 8.37832, 8.54170, -9.22522)

有人有创意的解决方案吗?任何回应都非常感谢!

3 个答案:

答案 0 :(得分:3)

val storeWithTransactions: String = Json.stringify(Json.obj(
       "store" -> storeJsValue,
       "transactions" -> transactionsJsValue))

1 .:用空格替换所有lat <- c("41º12'23.33''", "40º39'15.6'", "41 10 589", "38 31 10.6", "38.720647") lon <- c("8º19'40.66''", "7º52'31.95'", "8 37 832", "8 54 17.0", "-9.22522") gsub(" ", "", sub("\\s", ".", gsub("º|\\'|\\.", " ", lat))) [1] "41.122333" "40.39156" "41.10589" "38.31106" "38.720647" gsub(" ", "", sub("\\s", ".", gsub("º|\\'|\\.", " ", lon))) [1] "8.194066" "7.523195" "8.37832" "8.54170" "-9.22522" º'

2 .:用小数点替换第一个空格

3 .:用.替换所有剩余的空格,以再次将您的字符串粘贴在一起

答案 1 :(得分:1)

使用Base R可以请您关注并告诉我是否有帮助。

lat <- c("41º12'23.33''", "40º39'15.6'", "41 10 589", "38 31 10.6", "38.720647")

for (i in lat) 
{
  i <- gsub("º| ","@",i)
  i <- gsub("'|\\.","",i)
  i <- gsub("@",".",i)
  print(i)
}

输出如下。

[1] "41.122333"
[1] "40.39156"
[1] "41 10 589"
[1] "38 31 106"
[1] "38720647"

答案 2 :(得分:1)

此功能也将起作用:

# DATA
lat <- c("41º12'23.33''", "40º39'15.6'", "41 10 589", "38 31 10.6", "38.720647")
lon <- c("8º19'40.66''", "7º52'31.95'", "8 37 832", "8 54 17.0", "-9.22522")


# FUNCTION
convert_coordinates <- function(x) {
  splits <- x %>% strsplit(. , "º| |[.]|'") # Remove unwanted punctuation. Note that you can add more characters to replace here, just separate them with a |
  splits <- lapply(splits, function(x){x[!x ==""]}) # Remove any empty strings
  output <- c()
  for (i in 1:length(splits)) {
    output[i] <- paste0(splits[[i]][1], ".", paste0(splits[[i]][2:(length(splits[[i]]))], collapse=""), collapse="")
  }
  return(output)
}

# RESULTS
convert_coordinates(lat)
# [1] "41.122333" "40.39156"  "41.10589"  "38.31106"  "38.720647"

convert_coordinates(lon)
# [1] "8.194066" "7.523195" "8.37832"  "8.54170"  "-9.22522"