Kaushik
Kaushik

Reputation: 303

Concatenate multiple strings in R

I want to concatenate the below urls, I have written a below function to concatenate all the urls:

library(datetime)
library(lubridate)

get_thredds_url<- function(mon, hr){
  a <-"http://abc.co.in/"
  b <-"thredds/path/"
  c <-paste0("%02d", ymd_h(mon))
  d <-paste0(strftime(datetime_group, format="%Y%m%d%H"))
  e <-paste0("/gfs.t%sz.pgrb2.0p25.f%03d",(c, hr))
  url <-paste0(a,b,b,d)
  return (url)
}

mon = datetime(2017, 9, 26, 0)
hr = 240

url = get_thredds_url(mon,hr)
print (url)

But I am getting below error when I execute the definition of get_thredds_url():

Error: unexpected ',' in: " d<-paste0(strftime(datetime_group, format="%Y%m%d%H")) e<-paste0("/gfs.t%sz.pgrb2.0p25.f%03d",(c," url <-paste0(a,b,b,d)

Error in paste0(a, b, b, d) : object 'a' not found return (url) Error: no function to return from, jumping to top level } Error: unexpected '}' in "}"

What is wrong with my function and how can I solve this?

The final output should be:

http://abc.co.in/thredds/path/2017092600/gfs.t00z.pgrb2.0p25.f240

Upvotes: 0

Views: 3909

Answers (2)

KenHBS
KenHBS

Reputation: 7174

It was a bit messy to figure out what it is, you're trying to do. There seem to be quite a couple of contradicting pieces in your code, especially compared to your wanted final output. Therefore, I decided to focus on the wanted output and the inputs you provided in your variables.

get_thredds_url <- function(yr, mnth, day, hrs1, hrs2){
  part1 <- "http://abc.co.in/"
  part2 <- "thredds/path/"

  ymdh  <- c(yr, formatC(c(mnth, day, hrs1), width=2, flag="0"))
  part3 <- paste0(ymdh, collapse="")

  pre4  <- formatC(hrs1, width=2, flag="0")
  part4 <- paste0("/gfs.t", pre4, "z.pgrb2.0p25.f", hrs2)
  return(paste0(part1, part2, part3, part4))
}

get_thredds_url(2017, 9, 26, 0, 240)
# [1] "http://abc.co.in/thredds/path/2017092600/gfs.t00z.pgrb2.0p25.f240"

The key is using paste0() appropriately and I think formatC() may be new to some people (including me).

formatC() is used here to pad zeros in front of the number you provide, and thus makes sure that 9 is converted to 09, whereas 12 remains 12.

Note that this answer is in base R and does not require additional packages.

Also note that you should not use url and c as variable names. These names are already reserved for other functionalities in R. By using them as variable names, you are overwriting their actual purpose, which can (will) lead to problems at some point down the road

Upvotes: 1

manotheshark
manotheshark

Reputation: 4357

Using sprintf allows more control of values being inserted into string

library(lubridate)
get_thredds_url<- function(mon, hr){
  sprintf("http://abc.co.in/thredds/path/%s/gfs.t%02dz.pgrb2.0p25.f%03d",
          strftime(mon, format = "%Y%m%d%H", tz = "UTC"),
          hour(mon),
          hr)
}

mon <- make_datetime(2017, 9, 26, 0, tz = "UTC")
hr <- 240

get_thredds_url(mon, hr)
[1] "http://abc.co.in/thredds/path/2017092600/gfs.t00z.pgrb2.0p25.f240"

Upvotes: 1

Related Questions