Skip to content

Extending my code

10 messages · Ogbos Okike, Jeff Newmiller, Jim Lemon

#
Dear List,
I have a simple code with which I convert year, month, and day to a date format.
My data looks like:
67 01 26    18464
67 01 26    18472
67 01 26    18408
67 01 26    18360
67 01 26    18328
67 01 26    18320
67 01 26    18296

while my code is:


data <- read.table("CALG.txt", col.names = c("year", "month", "day", "counts"))
 new.century <- data$year < 50
data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
data$date <- as.Date(ISOdate(data$year, data$month, data$day))
x = data$date
 y = data$counts

I now wish to extend this code to be able to include hour for another
data of the format:
05 01 01 00    4009
05 01 01 01    3969
05 01 01 02    3946
05 01 01 03    3975
05 01 01 04    3960
05 01 01 05    3974
05 01 01 06    3971
05 01 01 07    3970
That is, I now would like to include hour in:
data <- read.table("CALG.txt", col.names = c("year", "month", "day", "counts")).

I am aware there are many other way of conversion but I have a
specific interest here. This code is a preamble to a larger code and
changing it to another format other than what I have will not be
compatible with the general code. Or will rather be difficult for me
to get another format fit into my main code.

So if you would be kind enough to assist me to run the read.table in the format:

data <- read.table("CALG.txt", col.names = c("year", "month",
"day","hour", "counts"))

and then run the rest as:

new.century <- data$year < 50
data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
data$date <- as.Date(ISOdate(data$year, data$month, data$day,data$hour))
x = data$date

such that year, month, day and hour will be stored in x,

I will be very thankful.

Thank you so much for your kind assistance.
Best regards
Ogbos
#
The Date class is not designed to handle time... you need to use the ISOdatetime function and convert to POSIXct instead of Date. Just be sure to set your timezone to some appropriate value before you convert any times into datetime types.

Sys.setenv( TZ="GMT" )
# avoid using `data` as that is the name of a base R function
dta <- read.table("CALG.txt", col.names = c("year", "month", "day", "hour", "counts"))
dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900)
dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))

I don't see why you feel obliged to copy the timestamp out of the data frame into x, but that is your business.

Appropriate timezone values can be reviewed with the OlsonNames() function.
On February 14, 2019 10:29:58 PM PST, Ogbos Okike <giftedlife2014 at gmail.com> wrote:

  
    
#
Dear Jeff,

Thank you so much.

I ran the code but got an error message. I then try to  run them line by line.

The problem is in:
dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
Error in with(dta, as.POSIXct(ISOdatetime(year, month, day, hour, 0, 0))) :
  object 'dta' not found

Thanks for another time.
Best
Ogbos
On Fri, Feb 15, 2019 at 7:51 AM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
#
Dear Jeff,

Please hold.
It is begging to work. There was an error somewhere. One ")" is
missing and as I went back to check the lines one by one with cursor,
I stubbed on non matching bracket.

I completed, run the code again and got some result.

Will get back to you once I am through.

Thanks in a hurry.
Best regards
Ogbos
On Fri, Feb 15, 2019 at 8:15 AM Ogbos Okike <giftedlife2014 at gmail.com> wrote:
#
Dear Jeff,
I am alright now!!!!

Please accept my indebtedness!!!

Warmest regards
Ogbos
On Fri, Feb 15, 2019 at 8:25 AM Ogbos Okike <giftedlife2014 at gmail.com> wrote:
1 day later
#
Dear Jeff,
One more problem please.

When I used as.Date(ISOdate(dta$year, dta$month, dta$day,dta$hour)) to
handle date, I could use text(as.Date("2005-03-13"),-9,"b") to label
my plot.

Now that I am using as.POSIXct(ISOdatetime(year,
month,day,hour,0,0))), can you please tell me how to text "b" on the
point corresponding with 1960-05-04 09:00:00 on my plot.

Many thanks for your extra time.

Best wishes
Ogbos
On Fri, Feb 15, 2019 at 8:25 AM Ogbos Okike <giftedlife2014 at gmail.com> wrote:
#
I have no idea how text(as.Date("2005-03-13"),-9,"b") would mark your plot anywhere near 1960-05-04 09:00:00. Perhaps someone else does. Or perhaps you can provide an actual minimal working example of what you had working before you changed to POSIXct.
On February 16, 2019 1:08:38 PM PST, Ogbos Okike <giftedlife2014 at gmail.com> wrote:

  
    
#
Hi Ogbos,
It may be easier to use strptime:

dta<-data.frame(year=rep(2005,5),month=rep("05",5),
 day=c("01","06","11","16","21"),
 hour=c(2,4,6,8,10),minute=rep(0,5),second=rep(0,5),value=1:5)
dta$Ptime<-strptime(paste(paste(dta$year,dta$month,dta$day,sep="-"),
 paste(dta$hour,dta$minute,dta$second,sep=":")),"%Y-%m-%d %H:%M:%S")
plot(dta$Ptime,dta$value)
text(strptime("2005-05-04 09:00:00","%Y-%m-%d %H:%M:%S"),4,"b")

Jim
#
Dear Jeff,
My error please and sorry about that.

 Not "1960-05-04 09:00:00".
I meant to write as.Date("2005-03-13"),-9,"b") and "2005-03-13 09:00:00".
My problem is the additional time factor.

I can text anywhere on my plot when dealing with yyyy/mm/dd but I can't
handle the new yyyy/mm/dd/hh/mm/ss.

Thank you for your patience.

Best regards
Ogbos.

I have no idea how text(as.Date("2005-03-13"),-9,"b") would mark your plot

  
  
#
Jim has pointed out the strptime function, but you can use as.POSIXct function also... so 

as.POSIXct( "2005-03-13 09:00:00" )
as.POSIXct( "03/13/2005 9:00", format="%m/%d/%Y %H:%M" )

or other variations should work.
On February 16, 2019 5:41:29 PM PST, Ogbos Okike <giftedlife2014 at gmail.com> wrote: