Is there a string formatting operator in R similar to Python's %?

后端 未结 3 1717
星月不相逢
星月不相逢 2021-02-01 15:44

I have a url that I need to send a request to using date variables. The https address takes the date variables. I\'d like to assign the dates to the address string using somethi

3条回答
  •  滥情空心
    2021-02-01 16:17

    The stringr package has the str_interp() function:

    year = "2008"
    mnth = "1"
    day = "31"
    stringr::str_interp("https:.../KBOS/${year}/${mnth}/${day}/DailyHistory.html")
    
    [1] "https:.../KBOS/2008/1/31/DailyHistory.html"
    

    or using a list (note that now numeric values are passed):

    stringr::str_interp("https:.../KBOS/${year}/${mnth}/${day}/DailyHistory.html", 
                                list(year = 2008, mnth = 1, day = 31))
    
    [1] "https:.../KBOS/2008/1/31/DailyHistory.html"
    

    BTW, formatting directives can also be passed, e.g., if the month fields needs to be two characters wide:

    stringr::str_interp("https:.../KBOS/${year}/$[02i]{mnth}/${day}/DailyHistory.html", 
                        list(year = 2008, mnth = 1, day = 31))
    
    [1] "https:.../KBOS/2008/01/31/DailyHistory.html"
    

提交回复
热议问题