0

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
8
  • first, both c and url are pre-defined functions in R, consider using better variable names. second, you have not defined datetime or ymd_h, are these loaded from a package? finally, in your line in the function where you define e try: e<-paste0("/gfs.t%sz.pgrb2.0p25.f%03d", c(cc, hr)). note that cc is what you call c in your function Commented Oct 3, 2017 at 14:41
  • I have used better variables names in my original code. And yes, datetime and ymd_h are loaded from the package. Commented Oct 3, 2017 at 14:46
  • 1
    datetime_group isn't defined either. please edit your question to be better Commented Oct 3, 2017 at 14:52
  • 1
    Be more specific than "loaded from the package". What package?? Commented Oct 3, 2017 at 14:55
  • 1
    Your first error is here: e<-paste0("/gfs.t%sz.pgrb2.0p25.f%03d",(c, hr)). You can't use (c,hr) like that - use e<-paste0("/gfs.t%sz.pgrb2.0p25.f%03d",c, hr). Let us know what happens once you change this. The other comments above are important too - make sure you read up on providing reproducible examples when posting on StackOverflow. Commented Oct 3, 2017 at 15:08

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

3 Comments

@montheshark, Thanks a lot.
@montheshark, I have one more query in continuation. I want to iterate over the mon and hr. Shall I write my question here?
@Kaushik any changes should be made to the original question. If it is a substantial change or unrelated to the original question then it should be asked as a new question. As a general rule, it is always helpful to show a small sample data set and desired output.
1

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

5 Comments

Thanks @Ken. it's working. I have one more question which is in continuation. Can I ask over here?
If it isn't mentioned in your question, it would be better if you open a new one. If it's about needing clarification regarding my answer, feel free to ask here
I just want to iterate over the months,year,hrs1 and hrs2. Final result would be I will be getting the list of URL's. If you can tell it here, it would be good.
@Kaushik if this answer was helpful consider accepting it as a solution (check mark to the left)
I'm sure you could have figured that out by yourself, too. I'm also not sure why you ask two answerers whether it's okay to top up your question, both say no, but you decide to go for it anyway..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.