regex - Fetch date time part from filename in R -
i want fetch date , time part filename given below:
filename<-"/abc/def/ghi/jk.0m25.2015011500.f264.path2.pathd254004.nc" i tried below code fetch desired part:
dt<- sub('^[^.]+.(\\d+)+[\\w\\d].*','\\1',filename)#not working here date , time part date:20150115 time:00
dtm <- strptime(c("2015-01-15 00:00:00"), format = "%y-%m-%d %h", tz = "est") then want add hours(which 264 in filename (/.f264.path2.)) dtm object.
final_dt<- dtm+264(hrs) final_dt contains both date , time.
once value of final_dt, want add column dataframe given below:
# creating dataframe long<-c(106.61291,-81.97224,-84.4277,-97.66631,-72.68604) lat<-c(35.04333,33.37378,33.64073,30.19743,41.93887) xy <- data.frame(long, lat) #desired output actual_date time final_date time long lat 1 2015-01-15 00 2015-01-26 ... 106.61291 35.04333 2 2015-01-15 00 2015-01-26 ... -81.97224 33.37378 3 2015-01-15 00 2015-01-26 ... -84.42770 33.64073 4 2015-01-15 00 2015-01-26 ... -97.66631 30.19743 5 2015-01-15 00 2015-01-26 ... -72.68604 41.93887
i'll try best clear up, let me start saying question unclear,
dt<- sub('^[^.]+.(\\d+)+[\\w\\d].*','\\1',filename)#not working
it appears asking solution part?
assuming date time sequence of 10 digits in row can appear in filename, can use following regex
"\.\d{10}\." then parse sub strings (yyyy, mm, dd, tt) date-time using substring function. , pass strptime method.
final_dt<- dtm+264(hrs) for addition/increment of time intervals, strptime appears accept integers seconds try
final_dt<- dtm + (264*60*60) where 60*60 60 minutes/hour * 60 seconds/minute unit conversion.
xy <- data.frame(long, lat) if have dtm's in vector, can use them @ time of creation of xy such as:
xy <- data.frame(dtm_vector, long, lat) (p.s. should try adding more tags question, such 'regex', since biggest hurdle in scenario)
Comments
Post a Comment