Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()?

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

This is what I would do in Python 2.7:

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

Or using .format() in 3.+.

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

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

Is there a better way of doing this?

like image 761
Conner M. Avatar asked Sep 06 '17 22:09

Conner M.


People also ask

What does %s mean in string?

It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string. The %s operator is put where the string is to be specified.

What is Percent R Python?

Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users. That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging. Here \n used to display to users doesn't work.

What is %s used for in Python strings?

Note 1: %s automatically converts numeric value to a string without throwing an error.

What is %s and %D Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values.


3 Answers

The equivalent in R is 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"
like image 162
LyzandeR Avatar answered Oct 03 '22 01:10

LyzandeR


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

Update: In stringr 1.2.0 they've added a wrapper function of 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
like image 31
austensen Avatar answered Oct 03 '22 02:10

austensen


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"
like image 36
Uwe Avatar answered Oct 03 '22 02:10

Uwe