1

I have the various file name as given below: file<-"/path/path/path/pth.0p25.2015011500.p264.path2.pathpathh254004.nc"

Problem 1: I want datetime part(2015011500)from the above file. I have written the below code:

# Fetching the date vlaue from the filename
a<-substr(gsub("[A-z]|[////]","",file),6,13)
 a
[1] "20150115"
hrs<-substr(gsub("[A-z]|[////]","",file),14,15)
hrs
[1] "00"
#concatenating both date and a as one
chr<-paste(a,hrs, sep=" ")
chr
[1] "20150115 00"

But when I am trying to convert chr to date. I am getting NULL value.

#Converting chr variable to date
datetime<-as.Date(a,format="%Y%m%d %H")

Result:
datetime
[1] NA

Problem 2: I want to fetch 264 from the file name. Code for this is given below:

validhrs<-substr(gsub("[A-z]|[////]","",ncfname),17,19)
Result:
validhrs
[1] "264"

I want to convert this validhr to time(hour).And then add this to datetime

Anyone could help me with this?

Thanks in advance!!

Kaushik
  • 303
  • 4
  • 11
  • you use `a` instead of `chr` in the `as.Date` function. If you use `chr` the `as.Date.` or `as.POSIXct` will work properly. – D.sen Sep 12 '17 at 20:15
  • For your frist problem, use `as.POSIXct` (which is for date-times) instead of `as.Date` (which is only for dates). Dates don't include hours and minutes. – Nathan Werth Sep 12 '17 at 20:15
  • FYI: [`[A-z]` matches more than just letters](http://stackoverflow.com/a/29771926/3832970). – Wiktor Stribiżew Sep 12 '17 at 20:15
  • @D.sen and @NathanWerth, I used as.POSIXct and I am getting the following value:`datetime<-as.POSIXct(chr,format="%Y%m%d %H%M%S") > datetime [1] NA`. Any Comments? – Kaushik Sep 12 '17 at 20:18
  • ^ Do not add the `%M%S`, your string does not reflect that format – D.sen Sep 12 '17 at 20:21
  • `datetime <- as.POSIXct(chr,format="%Y%m%d %H")` – D.sen Sep 12 '17 at 20:21
  • @D.Sen , thanks it worked. I am getting below output: `datetime [1] "2015-01-15 EST"`. Here, I am not getting time component. Is it because hour is 00 in my case? – Kaushik Sep 12 '17 at 20:22
  • Correct, switch `hrs` to `1` for testing purposes. You'll see the hour value. – D.sen Sep 12 '17 at 20:23
  • Thanks @D.sen. Could you help me with 2nd problem? – Kaushik Sep 12 '17 at 20:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154268/discussion-between-d-sen-and-kaushik). – D.sen Sep 12 '17 at 20:26

1 Answers1

1

Answer to Question 1: How to extract date from string, then convert it to date class:

x <- "/path/path/path/pth.0p25.2015011501.p264.path2.pathpathh254004.nc"

#Extract the date:
x <- gsub(".*(\\d{10}).*",'\\1',x)

#Convert to POSIXct:
x <- as.POSIXct(x,format="%Y%m%d %H")

Output:

[1] "2015-01-15 01:00:00 PST"

Answer to Question 2: How to add number of hours, "...p264..." in this case, to x:

hrs <- gsub(".*[p]+(\\d{3}).*","\\1",x)

require(lubridate)

x + hours(hrs)

Output:

[1] "2015-01-26 01:00:00 PST"

Note: 264 hours (i.e. 11 days) were added to what was once "2015-01-15 01:00:00".

www
  • 4,124
  • 1
  • 11
  • 22