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 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
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
Sent from my phone. Please excuse my brevity.
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:> > 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 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 > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, reproducible code. > > -- > Sent from my phone. Please excuse my brevity.