R中的字符串格式化运算符是否与Python的%相似?

时间:2021-01-22 20:15:02

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 something like the formatting operator % in Python. Does R have a similar operator or do I need to rely on paste()?

我有一个网址,我需要发送一个使用日期变量的请求。 https地址采用日期变量。我想使用像Python中的格式化运算符%那样将日期分配给地址字符串。 R有一个类似的运算符还是我需要依赖paste()?

# Example variables
year = "2008"
mnth = "1"
day = "31"

This is what I would do in Python 2.7:

这就是我在Python 2.7中要做的事情:

url = "https:.../KBOS/%s/%s/%s/DailyHistory.html" % (year, mnth, day)

Or using .format() in 3.+.

或者在3. +中使用.format()。

The only I'd know to do in R seems verbose and relies on paste:

在R中我唯一知道要做的事情似乎很冗长并且依赖于粘贴:

url_start = "https:.../KBOS/"
url_end = "/DailyHistory.html"
paste(url_start, year, "/", mnth, "/", day, url_end) 

Is there a better way of doing this?

有更好的方法吗?

3 个解决方案

#1


15  

The equivalent in R is sprintf:

R中的等价物是sprintf:

year = "2008"
mnth = "1"
day = "31"
url = sprintf("https:.../KBOS/%s/%s/%s/DailyHistory.html", year, mnth, day)
#[1] "https:.../KBOS/2008/1/31/DailyHistory.html"

Also, although I think it is an overkill, you could define an operator yourself too.

此外,虽然我认为这是一种矫枉过正,但您也可以自己定义一个操作员。

`%--%` <- function(x, y) {

  do.call(sprintf, c(list(x), y))

}

"https:.../KBOS/%s/%s/%s/DailyHistory.html" %--% c(year, mnth, day)
#[1] "https:.../KBOS/2008/1/31/DailyHistory.html"

#2


9  

As an alternative to sprintf, you might want to check out glue.

作为sprintf的替代品,您可能想要检查胶水。

Update: In stringr 1.2.0 they've added a wrapper function of glue::glue(), str_glue()

更新:在stringr 1.2.0中,他们添加了一个包装函数glue :: glue(),str_glue()


library(glue)

year = "2008"
mnth = "1"
day = "31"
url = glue("https:.../KBOS/{year}/{mnth}/{day}/DailyHistory.html")

url

#> https:.../KBOS/2008/1/31/DailyHistory.html

#3


3  

The stringr package has the str_interp() function:

stringr包具有str_interp()函数:

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"

#1


15  

The equivalent in R is sprintf:

R中的等价物是sprintf:

year = "2008"
mnth = "1"
day = "31"
url = sprintf("https:.../KBOS/%s/%s/%s/DailyHistory.html", year, mnth, day)
#[1] "https:.../KBOS/2008/1/31/DailyHistory.html"

Also, although I think it is an overkill, you could define an operator yourself too.

此外,虽然我认为这是一种矫枉过正,但您也可以自己定义一个操作员。

`%--%` <- function(x, y) {

  do.call(sprintf, c(list(x), y))

}

"https:.../KBOS/%s/%s/%s/DailyHistory.html" %--% c(year, mnth, day)
#[1] "https:.../KBOS/2008/1/31/DailyHistory.html"

#2


9  

As an alternative to sprintf, you might want to check out glue.

作为sprintf的替代品,您可能想要检查胶水。

Update: In stringr 1.2.0 they've added a wrapper function of glue::glue(), str_glue()

更新:在stringr 1.2.0中,他们添加了一个包装函数glue :: glue(),str_glue()


library(glue)

year = "2008"
mnth = "1"
day = "31"
url = glue("https:.../KBOS/{year}/{mnth}/{day}/DailyHistory.html")

url

#> https:.../KBOS/2008/1/31/DailyHistory.html

#3


3  

The stringr package has the str_interp() function:

stringr包具有str_interp()函数:

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"