替换R中的特殊字符
Posted
技术标签:
【中文标题】替换R中的特殊字符【英文标题】:Substituting special characters in R 【发布时间】:2016-06-05 07:35:53 【问题描述】:我有一个数据文件,其地理坐标以度-分-秒表示,结构如下:
39°47′01″ N,100°26′45″ W
我可以通过以下方式将其读入 R:
d <- read.csv("D:/locations.csv", encoding="UTF-8")
head(d)
Location Lat Long
United States 39°47'01<U+2033> N 100°26'45<U+2033> W
Denmark 55°40'13<U+2033> N 10°20'00<U+2033> E
.
.
.
我需要将坐标转换为十进制度。我正在使用替换将其转换为sp::char2dms
所需的32d14'23" N
格式。我正在使用以下替换,它适用于度数和秒数,但不适用于分钟部分:
d$Lat %>% sub("°", "d", .) %>% sub("′", "m", .) %>% sub("″","s",.)
[1] "39d47′01s N "35d00′00s N" "55d40′13s N"
我应该如何替换会议记录?我已经尝试在 ' 之前使用转义序列。
更新
根据评论中的要求,这里是使用 dput 的数据示例:
> dput(sample(d$Lat, 10))
structure(c(174L, 7L, 185L, 121L, 127L, 200L, 121L, 62L, 64L,
191L), .Label = c("", "0°18'22<U+2033> N", "0°31'30<U+2033> S",
"0°53'15<U+2033> N", "0°54'00<U+2033> S", "1°17'26<U+2033> N",
"1°20'23<U+2033> S", "1°26'31<U+2033> N", "1°32'00<U+2033> N",
"1°36'47<U+2033> N", "1°57'53<U+2033> S", "10°12'42<U+2033> N",
"10°16'25<U+2033> N", "10°20'00<U+2033> S", "10°43'21<U+2033> N",
"10°52'04<U+2033> N", "11°51'24<U+2033> N", "11°52'39<U+2033> S",
"12°04'31<U+2033> N", "12°06'00<U+2033> N", "12°08'10<U+2033> N",
"12°09'17<U+2033> N", "12°12'16<U+2033> S", "12°22'21<U+2033> N",
"12°30'05<U+2033> N", "12°45'01<U+2033> N", "12°54'16<U+2033> N",
"13°09'00<U+2033> N", "13°16'07<U+2033> S", "13°17'25<U+2033> N",
"13°28'12<U+2033> N", "13°30'24<U+2033> N", "13°46'10<U+2033> S",
"13°48'00<U+2033> N", "13°49'30<U+2033> N", "14°28'30<U+2033> N",
"14°31'07<U+2033> S", "14°53'50<U+2033> N", "15°03'40<U+2033> N",
"15°24'41<U+2033> N", "15°27'13<U+2033> N", "15°36'48<U+2033> N",
"15°38'08<U+2033> N", "15°57'00<U+2033> N", "16°00'00<U+2033> N",
"16°20'50<U+2033> N", "16°22'12<U+2033> N", "16°31'32<U+2033> S",
"16°49'34<U+2033> N", "17°03'25<U+2033> S", "17°04'45<U+2033> N",
"17°10'30<U+2033> N", "17°20'46<U+2033> N", "17°44'08<U+2033> N",
"18°07'26<U+2033> S", "18°11'06<U+2033> N", "18°27'20<U+2033> S",
"18°55'30<U+2033> S", "19°05'51<U+2033> N", "19°08'24<U+2033> N",
"19°18'08<U+2033> S", "19°25'57<U+2033> N", "19°54'58<U+2033> S",
"2°23'33<U+2033> N", "2°53'35<U+2033> N", "2°58'53<U+2033> S",
"20°01'02<U+2033> N", "20°15'15<U+2033> N", "20°16'33<U+2033> S",
"21°00'00<U+2033> N", "22°21'04<U+2033> N", "23°00'47<U+2033> N",
"23°10'05<U+2033> S", "23°14'01<U+2033> S", "23°19'00<U+2033> S",
"23°58'26<U+2033> N", "24°00'01<U+2033> N", "24°28'37<U+2033> N",
"24°46'25<U+2033> N", "24°46'34<U+2033> S", "25°13'06<U+2033> N",
"25°13'41<U+2033> N", "25°13'57<U+2033> N", "25°37'27<U+2033> N",
"26°09'18<U+2033> N", "26°15'15<U+2033> N", "26°33'45<U+2033> S",
"26°49'24<U+2033> N", "27°32'58<U+2033> N", "28°00'00<U+2033> N",
"28°06'30<U+2033> N", "28°49'00<U+2033> S", "29°16'24<U+2033> N",
"29°36'14<U+2033> S", "3°21'48<U+2033> S", "30°19'51<U+2033> N",
"30°52'34<U+2033> N", "31°10'00<U+2033> N", "31°10'22<U+2033> N",
"31°25'59<U+2033> N", "31°45'41<U+2033> S", "31°59'28<U+2033> N",
"32°01'32<U+2033> N", "32°19'46<U+2033> N", "32°52'32<U+2033> S",
"32°56'27<U+2033> N", "33°00'00<U+2033> N", "33°05'44<U+2033> N",
"33°50'38<U+2033> N", "33°52'30<U+2033> N", "34°38'25<U+2033> N",
"34°58'56<U+2033> N", "34°59'47<U+2033> S", "35°00'00<U+2033> N",
"35°23'55<U+2033> N", "36°34'29<U+2033> N", "38°37'41<U+2033> N",
"38°57'35<U+2033> N", "39°00'00<U+2033> N", "39°22'35<U+2033> N",
"39°47'01<U+2033> N", "4°08'29<U+2033> N", "4°13'19<U+2033> N",
"4°24'49<U+2033> N", "4°36'45<U+2033> N", "4°39'27<U+2033> S",
"4°47'58<U+2033> S", "4°50'30<U+2033> N", "40°00'10<U+2033> N",
"40°02'00<U+2033> N", "40°18'45<U+2033> N", "40°23'37<U+2033> N",
"40°45'41<U+2033> N", "40°46'11<U+2033> N", "41°00'00<U+2033> N",
"41°19'25<U+2033> N", "41°30'00<U+2033> S", "41°30'32<U+2033> N",
"41°37'02<U+2033> N", "42°11'08<U+2033> N", "42°36'27<U+2033> N",
"42°38'18<U+2033> N", "42°45'09<U+2033> N", "43°01'43<U+2033> N",
"43°43'52<U+2033> N", "44°09'04<U+2033> S", "44°18'19<U+2033> N",
"45°33'52<U+2033> N", "45°48'48<U+2033> N", "45°59'07<U+2033> N",
"46°32'01<U+2033> N", "46°36'12<U+2033> N", "46°47'55<U+2033> N",
"46°49'30<U+2033> N", "47°10'54<U+2033> N", "47°12'00<U+2033> N",
"47°13'43<U+2033> N", "47°17'12<U+2033> N", "47°31'02<U+2033> N",
"48°44'28<U+2033> N", "49°29'14<U+2033> N", "49°49'00<U+2033> N",
"5°33'36<U+2033> N", "5°40'54<U+2033> S", "5°45'00<U+2033> N",
"50°38'27<U+2033> N", "51°05'00<U+2033> N", "51°31'14<U+2033> N",
"52°05'52<U+2033> N", "52°14'17<U+2033> N", "52°51'55<U+2033> N",
"53°25'30<U+2033> N", "54°10'37<U+2033> S", "54°42'08<U+2033> N",
"55°21'00<U+2033> N", "55°40'13<U+2033> N", "58°45'09<U+2033> N",
"59°40'30<U+2033> N", "6°05'51<U+2033> N", "6°31'29<U+2033> S",
"6°52'12<U+2033> S", "6°57'07<U+2033> N", "61°04'00<U+2033> N",
"64°34'27<U+2033> N", "64°41'11<U+2033> N", "64°59'03<U+2033> N",
"69°10'21<U+2033> S", "7°52'41<U+2033> N", "7°59'23<U+2033> N",
"8°00'07<U+2033> N", "8°01'48<U+2033> N", "8°18'35<U+2033> N",
"8°22'04<U+2033> N", "8°30'55<U+2033> S", "8°38'24<U+2033> N",
"8°46'48<U+2033> N", "82°39'44<U+2033> N", "9°31'46<U+2033> N",
"9°36'00<U+2033> N", "9°44'08<U+2033> S"), class = "factor")
【问题讨论】:
能否分享dput(head(d))
的结果以作为reproducible 的示例?
【参考方案1】:
我们可以在单个 sub
和捕获组中完成此操作
sub("(\\d+).(\\d+).(\\d+).*(\\s+.)", "\\1d\\2m\\3s\\4", v1)
#[1] "54d42m08s N" "1d20m23s S" "64d41m11s N" "39d47m01s N" "4d47m58s S"
#[6] "9d44m08s S" "39d47m01s N" "19d25m57s N" "2d23m33s N" "8d01m48s N"
其中“v1”是d$Lat
。
在这里,我们没有使用%>%
,因为它不是必需的。
【讨论】:
【参考方案2】:使用您的示例,您可以尝试以下操作(应为 d$Lat
和您的 data.frame
):
d %>% sub("°", "d", .) %>% sub("'", "m", .) %>% sub("<U\\+2033>","s",.)
[1] "54d42m08s N" "1d20m23s S" "64d41m11s N" "39d47m01s N" "4d47m58s S"
[6] "9d44m08s S" "39d47m01s N" "19d25m57s N" "2d23m33s N" "8d01m48s N"
但我认为可能有一种更聪明的方法(不用*sub
在坐标系之间进行转换)。
【讨论】:
以上是关于替换R中的特殊字符的主要内容,如果未能解决你的问题,请参考以下文章