替换R中字符串中的每秒逗号

时间:2017-05-24 23:50:41

标签: r regex google-maps-api-3

我有一个表示lat,long坐标的字符串,显示如下:

abstract class OnEvent {
  /// Called when an event happens.
  void onEvent();
}

@Component(
  selector: 'a',
  directives: const [B],
  template: '<b></b>',
  providers: const [
    const Provider(OnEvent, useExisting: A),
  ],
)
class A implements OnEvent {
  @override
  void onEvent() {
    print('>>> An event was triggered!');
  }
}

class C {
  final OnEvent _eventHandler;

  C(this._eventHandler);

  void onSomeAction() {
    _eventHandler.onEvent();
  }
}

我需要用30.538358,-96.692008,30.538602,-96.691741,30.539737,-96.690322

替换每一秒逗号
|

我需要这个,因为Google MAPS提升服务需要这种格式。

2 个答案:

答案 0 :(得分:4)

您可以使用string = "30.538358,-96.692008,30.538602,-96.691741,30.539737,-96.690322" gsub("([^,]+,[^,]+),", "\\1|", string) "30.538358,-96.692008|30.538602,-96.691741|30.539737,-96.690322" 和正则表达式执行此操作。

for i in [phil, macie, sashi, roxie, darla, sammy]:
    mr.setColor()
    mr.setX(xy)
    mr.setY(xy)
    mr.setWidth(wh)
    mr.setHeight(wh)

答案 1 :(得分:0)

在R中,您可以使用我的google_elevation()包中的googleway功能来调用Google的Elevation API

df <- read.table(text = "lat,lon
                 30.538358,-96.692008
                 30.538602,-96.691741
                 30.539737,-96.690322",header=T,sep = ",")

google_elevation(df_locations = df, key = apiKey)
# $results
# elevation location.lat location.lng resolution
# 1  101.8991     30.53836    -96.69201   9.543952
# 2  102.0905     30.53860    -96.69174   9.543952
# 3  101.2652     30.53974    -96.69032   9.543952
# 
# $status
# [1] "OK"

或者,如果您有更多分数,最好将它们编码为折线,这样就不会达到API查询限制as found here

要使用此方法,您需要包的开发版本

## devtools::install_github("SymbolixAU/googleway")
## library(googleway)
pl <- encode_pl(lat = df$lat, lon = df$lon)
pl
# "uokyD~cdmQq@s@aF{G"
google_elevation(polyline = pl, key = apiKey)
# $results
# elevation location.lat location.lng resolution
# 1  101.9124     30.53835    -96.69200   9.543952
# 2  102.0864     30.53860    -96.69174   9.543952
# 3  101.1588     30.53973    -96.69032   9.543952
# 
# $status
# [1] "OK"