Re: [R] Convert character date time to R date-time variable.

2023-12-08 Thread Duncan Murdoch

On 08/12/2023 4:30 p.m., David Winsemius wrote:


On 12/7/23 08:21, Sorkin, John wrote:

Colleagues,

I have a matrix of character data that represents date and time. The format of 
each element of the matrix is
"2020-09-17_00:00:00"
How can I convert the elements into a valid R date-time constant?


You will not be able to store these datetime values in an R matrix, at
least as class POSIXct. You could with class POSIXlt, but I've not seen
it used before but it does appear possible since matrices can contain
lists.

R matrices do no provide the capability to assign attributes, so only
atomic types and lists can be elements. If you wanted to maintain the
same structure, your first step might be to coerce to a data.frame and
then proceed, or to first construct a vector and then use as the first
argument to `matrix`.


The general sentiment is correct (it's hard to put POSIXct elements in 
matrices), but it's not impossible.  Almost all R objects can have 
attributes.


For example,

  m <- matrix(Sys.time(), 2,2)

converts the time to a numerical value, but you can view it as a POSIXlt 
object using


  class(m) <- c("POSIXct", class(m))

Subsetting works, e.g. m[1,1] is a time, etc.

Duncan Murdoch



Best;

David



Thank you,
John



John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;

Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;

PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;

Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



__
R-help@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.


__
R-help@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.


__
R-help@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.


Re: [R] Convert character date time to R date-time variable.

2023-12-08 Thread David Winsemius



On 12/7/23 08:21, Sorkin, John wrote:

Colleagues,

I have a matrix of character data that represents date and time. The format of 
each element of the matrix is
"2020-09-17_00:00:00"
How can I convert the elements into a valid R date-time constant?


You will not be able to store these datetime values in an R matrix, at 
least as class POSIXct. You could with class POSIXlt, but I've not seen 
it used before but it does appear possible since matrices can contain 
lists.


R matrices do no provide the capability to assign attributes, so only 
atomic types and lists can be elements. If you wanted to maintain the 
same structure, your first step might be to coerce to a data.frame and 
then proceed, or to first construct a vector and then use as the first 
argument to `matrix`.



Best;

David



Thank you,
John



John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;

Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;

PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;

Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



__
R-help@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.


__
R-help@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.


Re: [R] Convert character date time to R date-time variable.

2023-12-08 Thread Martin Maechler
>>>>> Ebert,Timothy Aaron 
>>>>> on Thu, 7 Dec 2023 16:29:09 + writes:

> Look at the lubridate package in R.  Regards, Tim

Absolutely *un*needed here !! - as others mention in this
thread.

Very simple with base R:

  > strptime("2020-09-17_00:00:00", format = "%Y-%m-%d_%H:%M:%S")
  [1] "2020-09-17 CEST"
  > 

(in my time zone).
 
> -Original Message- From: R-help
>  On Behalf Of Sorkin, John
> Sent: Thursday, December 7, 2023 11:22 AM To:
> r-help@r-project.org (r-help@r-project.org)
>  Subject: [R] Convert character date
> time to R date-time variable.

> [External Email]

> Colleagues,

> I have a matrix of character data that represents date and
> time. The format of each element of the matrix is
> "2020-09-17_00:00:00" How can I convert the elements into
> a valid R date-time constant?

> Thank you, John



> John David Sorkin M.D., Ph.D.  Professor of Medicine,
> University of Maryland School of Medicine;

> Associate Director for Biostatistics and Informatics,
> Baltimore VA Medical Center Geriatrics Research,
> Education, and Clinical Center;

> PI Biostatistics and Informatics Core, University of
> Maryland School of Medicine Claude D. Pepper Older
> Americans Independence Center;

> Senior Statistician University of Maryland Center for
> Vascular Research;

> Division of Gerontology and Paliative Care, 10 North
> Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524
> Cell phone 443-418-5382



> __
> R-help@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.

> __
> R-help@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.

__
R-help@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.


Re: [R] Convert character date time to R date-time variable.

2023-12-07 Thread Dirk Eddelbuettel


`anytime` was written for this:

  > anytime::anytime("2020-09-17_00:00:00")
  [1] "2020-09-17 CDT"
  > class(anytime::anytime("2020-09-17_00:00:00"))
  [1] "POSIXct" "POSIXt" 
  > 

Dirk


-- 
dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-help@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.


Re: [R] Convert character date time to R date-time variable.

2023-12-07 Thread Rui Barradas

Às 16:30 de 07/12/2023, Rui Barradas escreveu:

Às 16:21 de 07/12/2023, Sorkin, John escreveu:

Colleagues,

I have a matrix of character data that represents date and time. The 
format of each element of the matrix is

"2020-09-17_00:00:00"
How can I convert the elements into a valid R date-time constant?

Thank you,
John



John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;

Associate Director for Biostatistics and Informatics, Baltimore VA 
Medical Center Geriatrics Research, Education, and Clinical Center;


PI Biostatistics and Informatics Core, University of Maryland School 
of Medicine Claude D. Pepper Older Americans Independence Center;


Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



__
R-help@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.

Hello,

Coerce with ?as.POSIXct
Don't forget the underscore in the format.


as.POSIXct("2020-09-17_00:00:00", format = "%Y-%m-%d_%H:%M:%S")


Hope this helps,

Rui Barradas



Sorry, I forgot:


lubridate::ymd_hms("2020-09-17_00:00:00")


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
R-help@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.


Re: [R] Convert character date time to R date-time variable.

2023-12-07 Thread Rui Barradas

Às 16:21 de 07/12/2023, Sorkin, John escreveu:

Colleagues,

I have a matrix of character data that represents date and time. The format of 
each element of the matrix is
"2020-09-17_00:00:00"
How can I convert the elements into a valid R date-time constant?

Thank you,
John



John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;

Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;

PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;

Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



__
R-help@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.

Hello,

Coerce with ?as.POSIXct
Don't forget the underscore in the format.


as.POSIXct("2020-09-17_00:00:00", format = "%Y-%m-%d_%H:%M:%S")


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
R-help@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.


Re: [R] Convert character date time to R date-time variable.

2023-12-07 Thread Ebert,Timothy Aaron
Look at the lubridate package in R.
Regards,
Tim

-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Thursday, December 7, 2023 11:22 AM
To: r-help@r-project.org (r-help@r-project.org) 
Subject: [R] Convert character date time to R date-time variable.

[External Email]

Colleagues,

I have a matrix of character data that represents date and time. The format of 
each element of the matrix is "2020-09-17_00:00:00"
How can I convert the elements into a valid R date-time constant?

Thank you,
John



John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;

Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;

PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;

Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



__
R-help@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.

__
R-help@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.


[R] Convert character date time to R date-time variable.

2023-12-07 Thread Sorkin, John
Colleagues,

I have a matrix of character data that represents date and time. The format of 
each element of the matrix is 
"2020-09-17_00:00:00"
How can I convert the elements into a valid R date-time constant?

Thank you,
John



John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;

Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center; 

PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;

Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



__
R-help@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.


Re: [R] Date order question

2023-01-04 Thread PIKAL Petr
Hallo Thomas

Similar as suggested by Rui, you shall change your date to real date e.g. by

library(lubridate)
date <- paste(date, c(rep(2022,2), 2023), sep="-")
date <- mdy(date)

and you need to change also x coordinate in annotate.

ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
 
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.
2))+
  annotate("text", x=date[2], y=.1, label="Very
Good",size=5,fontface="bold")+
  annotate("text", x=date[2], y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=date[2], y=.28,
label="Marginal",size=5,fontface="bold") +
  annotate("text", x=date[2], y=.45,
label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=date[2], y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=date[2], y=-.05, label="PT Not
Done",size=5,fontface="bold")

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Thomas Subia
> Sent: Wednesday, January 4, 2023 10:08 PM
> To: r-help@r-project.org
> Subject: [R] Date order question
> 
> Colleagues,
> 
> date<-c("12-29","12-30","01-01")
> PT <- c(.106,.130,.121)
> data <- data.frame(date,PT)
> ggplot(data, aes(x=date,y=PT,group=1))+
>   geom_point(size=4)+
>   geom_line()+
>   geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
> 
>
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.
2))
> +
>   annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
>   annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
>   annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold")
+
>   annotate("text", x=2.5, y=.45,
label="Inadequate",size=6,fontface="bold")+
>   annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
>   annotate("text", x=2.5, y=-.05, label="PT Not
Done",size=5,fontface="bold")+
>   theme_cowplot()
> 
> The plot has the wrong date order.
> What is desired is 12-29, 12-30 and 01-01.
> 
> Some feedback would be appreciated.
> 
> All the best,
> Thomas Subia
> 
> "De quoi devenir chevre? Des donnees"
> 
> __
> R-help@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.
__
R-help@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.


Re: [R] Date order question

2023-01-04 Thread CALUM POLWART
On Wed, 4 Jan 2023, 21:29 Ebert,Timothy Aaron,  wrote:

>
> As you are plotting strings, you could put a space character in front of
> the December dates so that they are first.
> date<-c(" 12-29"," 12-30","01-01")
> That fixes the problem in this example. You can order all the dates by
> putting more spaces in front of earlier years. That will get messy.
>

Put the year in front +/- apply as.Date() and you would be fine...

date<-c("2022-12-29","2022-12-30","2023-01-01") |> as.Date()

It may be that the source data doesn't have a year and the example given is
to show us dummy data.  You could 'automate' the addition along the lines
of:

require(tidyverse)
#if you have up to date Tidyverse this includes lubridate
current_date <- sys.Date()
current_month <- month(current_date)
current_year <- year(current_date)
date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
data |>
  # separate the date into month and day column
  separate (date, c("Month", "Day"), sep="-") |>
  # add a year if month is > current month must be last year
 mutate (year = if_else(Month > current_month, current_year - 1,
current_year)) |>
  #rebuild the date
  unite (date, c("Year", "Month", "Day"), sep="-") |>
  mutate(date = as.Date(date)) -> data

If you don't want year on the axis of the graph, that should be dealt with
in ggplot not in the data carpentry

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date order question

2023-01-04 Thread Rui Barradas

Às 21:08 de 04/01/2023, Thomas Subia escreveu:

Colleagues,

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
   geom_point(size=4)+
   geom_line()+
   geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
   
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
   annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
   annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
   annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
   annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
   annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
   annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
   theme_cowplot()

The plot has the wrong date order.
What is desired is 12-29, 12-30 and 01-01.

Some feedback would be appreciated.

All the best,
Thomas Subia

"De quoi devenir chevre? Des donnees"

__
R-help@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.

Hello,

Transform `date` into a real date column in a dplyr pipe so that the 
original data remains unchanged and plot with scale_x_date which will 
allow for control over the axis breaks and labels.


The only problem are now the text annotations. The x coordinate is 
numeric and it must also be a date object. Below I solve this by 
creating a annotations data set ann_data. In geom_text the data argument 
is now another one, but one layer only annotates all areas.



library(ggplot2)
library(cowplot)
library(dplyr)

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)

ann_data <- data.frame(
  y = c(0.1, 0.225, 0.28, 0.45, 0.8, -0.05),
  text = c("Very Good", "Good", "Marginal", "Inadequate", "OOC", "PT 
Not Done")

)
ann_data$x <- as.Date("2022-12-31")


data %>%
  mutate(newyear = ifelse(substr(date, 1, 2) > "01", "2022-", "2023-"),
 date = as.Date(paste0(newyear, date))) %>%
  ggplot(aes(x = date, y = PT)) +
  geom_point(size = 4) +
  geom_line() +
  geom_hline(yintercept = c(1,.60,0,.30,.25,.2)) +
  #
  geom_text(
data = ann_data,
mapping = aes(x = x, y = y, label = text),
size = 5, fontface = "bold"
  ) +
  scale_x_date(date_breaks = "1 day", date_labels = "%m-%d") +
  #

scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  theme_cowplot()



Hope this helps,

Rui Barradas

__
R-help@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.


Re: [R] Date order question

2023-01-04 Thread Ebert,Timothy Aaron
1) Your "dates" are not being processed as dates. They are strings.
2) As written your date "01-01" comes first because year is not specified and 
January comes before December.
3) A library statement is missing. I tried running the program and 
theme_cowplot() was not found.

As you are plotting strings, you could put a space character in front of the 
December dates so that they are first. 
date<-c(" 12-29"," 12-30","01-01")
That fixes the problem in this example. You can order all the dates by putting 
more spaces in front of earlier years. That will get messy.


Tim

-Original Message-
From: R-help  On Behalf Of Thomas Subia
Sent: Wednesday, January 4, 2023 4:08 PM
To: r-help@r-project.org
Subject: [R] Date order question

[External Email]

Colleagues,

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()

The plot has the wrong date order.
What is desired is 12-29, 12-30 and 01-01.

Some feedback would be appreciated.

All the best,
Thomas Subia

"De quoi devenir chevre? Des donnees"

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7Ctebert%40ufl.edu%7C9f58ce4e64d04d2b3f0908daee97dcb5%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638084633565926697%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=y7Co7Ae8RJG4AWxjwLLNcvopfUt7fGRBguUovhYaxl8%3D=0
PLEASE do read the posting guide 
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7Ctebert%40ufl.edu%7C9f58ce4e64d04d2b3f0908daee97dcb5%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638084633565926697%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=pTYhbgrmoAu6g3%2FDepzJJt45Euazik4D6UJKByOZKuQ%3D=0
and provide commented, minimal, self-contained, reproducible code.

__
R-help@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.


Re: [R] Date order question

2023-01-04 Thread Andrew Simmons
I converted `date` to a factor and it seemed to work:


```
library(ggplot2)
library(cowplot)
date <- c("12-29","12-30","01-01")
date <- factor(date, labels = unique(date))
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()
```

On Wed, Jan 4, 2023 at 4:09 PM Thomas Subia
 wrote:
>
> Colleagues,
>
> date<-c("12-29","12-30","01-01")
> PT <- c(.106,.130,.121)
> data <- data.frame(date,PT)
> ggplot(data, aes(x=date,y=PT,group=1))+
>   geom_point(size=4)+
>   geom_line()+
>   geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
>   
> scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
>   annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
>   annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
>   annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
>   annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
>   annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
>   annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
>   theme_cowplot()
>
> The plot has the wrong date order.
> What is desired is 12-29, 12-30 and 01-01.
>
> Some feedback would be appreciated.
>
> All the best,
> Thomas Subia
>
> "De quoi devenir chevre? Des donnees"
>
> __
> R-help@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.

__
R-help@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.


[R] Date order question

2023-01-04 Thread Thomas Subia
Colleagues,

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()

The plot has the wrong date order.
What is desired is 12-29, 12-30 and 01-01.

Some feedback would be appreciated.

All the best,
Thomas Subia

"De quoi devenir chevre? Des donnees"

__
R-help@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.


Re: [R] Date and Time

2022-07-18 Thread Ebert,Timothy Aaron
An alternative is the lubridate() package if you use tidyverse.

Check the data for daylight savings time. Twice per year Virginia changes time 
where in the Fall clocks are shifted back an hour and in the Spring they jump 
forwards an hour.

-Original Message-
From: R-help  On Behalf Of Jeff Newmiller
Sent: Monday, July 18, 2022 12:35 AM
To: Greg Comcast Coats ; Gregory Coats via R-help 

Subject: Re: [R] Date and Time

[External Email]

Maybe this [1] will help? Or just read ?strptime...

You can also calculate sunrise/sunset (crepuscule) using maptools, but you need 
to be careful with timezones.

[1] 
https://urldefense.proofpoint.com/v2/url?u=https-3A__jdnewmil.github.io_time-2D2018-2D10_MoreDatetimeHowto.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3XGPiP=O5VruA8vvqyhQngUZ3fuZtnZiYIm9XEscpzxcGKX9AQ=

On July 17, 2022 8:47:19 PM PDT, Gregory Coats via R-help 
 wrote:
>For the year from 2022-01-01 to 2022-12-31, I computed the sunrise and sunset 
>times for Reston, Virginia, USA. I am seeking the syntax to direct R to read 
>in these dates and times, and then graphical plot the sunrise and sunset times 
>for each day in the year 2022. How do I tell R that I store the Sunrise Date 
>and Time like this?
>-mm-dd HH:MM:SS
>2022-01-01  7:28:10
>2022-01-02  7:28:17
>Greg Coats
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
>an_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRz
>sn7AkP-g=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3X
>GPiP=7XAM9jGAgm-2KbQG5x3oKKxrvNESM5DAvl_O3YxSU3Y=
>PLEASE do read the posting guide 
>https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
>_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsR
>zsn7AkP-g=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3
>XGPiP=MwaLmYJXfAwQDsHSzq8Atm-QC2YpJYie_L5oNkAJiVg=
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3XGPiP=7XAM9jGAgm-2KbQG5x3oKKxrvNESM5DAvl_O3YxSU3Y=
PLEASE do read the posting guide 
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=Eym42IWzdKGREg7m9o0QowA8hP-DgHTrlZwDfW5no2rP6ND_uFJKHbJ_Dj3XGPiP=MwaLmYJXfAwQDsHSzq8Atm-QC2YpJYie_L5oNkAJiVg=
and provide commented, minimal, self-contained, reproducible code.

__
R-help@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.


Re: [R] Date and Time

2022-07-17 Thread Daniel Nordlund

On 7/17/2022 8:47 PM, Gregory Coats via R-help wrote:

For the year from 2022-01-01 to 2022-12-31, I computed the sunrise and sunset 
times for Reston, Virginia, USA. I am seeking the syntax to direct R to read in 
these dates and times, and then graphical plot the sunrise and sunset times for 
each day in the year 2022. How do I tell R that I store the Sunrise Date and 
Time like this?
-mm-dd HH:MM:SS
2022-01-01  7:28:10
2022-01-02  7:28:17
Greg Coats
[[alternative HTML version deleted]]

__
R-help@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.


Here is one way using the strptime function,

> in_dt_str <- '2022-01-01  7:28:10'
> dt <- strptime(in_dt_str,'%Y-%m-%d %H:%M:%S')

if the date and time are stored in different variables then paste them 
together before converting


> in_dt <- '2022-01-01'
> in_tm <- '7:28:10'
> dtm <- strptime(paste(in_dt, in_tm, sep=' '),'%Y-%m-%d %H:%M:%S')

Hope this is helpful,

Dan

--
Daniel Nordlund
Port Townsend, WA  USA


--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

__
R-help@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.


Re: [R] Date and Time

2022-07-17 Thread Jeff Newmiller
Maybe this [1] will help? Or just read ?strptime...

You can also calculate sunrise/sunset (crepuscule) using maptools, but you need 
to be careful with timezones.

[1] https://jdnewmil.github.io/time-2018-10/MoreDatetimeHowto.html

On July 17, 2022 8:47:19 PM PDT, Gregory Coats via R-help 
 wrote:
>For the year from 2022-01-01 to 2022-12-31, I computed the sunrise and sunset 
>times for Reston, Virginia, USA. I am seeking the syntax to direct R to read 
>in these dates and times, and then graphical plot the sunrise and sunset times 
>for each day in the year 2022. How do I tell R that I store the Sunrise Date 
>and Time like this?
>-mm-dd HH:MM:SS
>2022-01-01  7:28:10
>2022-01-02  7:28:17
>Greg Coats
>   [[alternative HTML version deleted]]
>
>__
>R-help@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.

__
R-help@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.


Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-23 Thread peter dalgaard
It reads other formats _if you specify them_. After all, no computer (or human) 
can tell whether 11/03/1959 is November 3 or March 11 without further hinting. 
So it tries the two ISO-like formats and leaves other cases for the user.

-pd

> On 20 Nov 2021, at 21:22 , Philip Monk  wrote:
> 
> Thanks, Andrew.  I didn't realise as.Date *only* read two formats, I
> think I was tripped up by using %y instead of %Y, 

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-22 Thread Spencer Graves
	  I've written Ecfun::asNumericDF to overcome some of the common 
problems with read.data, read.csv, etc.:



https://www.rdocumentation.org/packages/Ecfun/versions/0.2-5/topics/asNumericDF


	  I use it routinely to help parse numbers, dates, etc., that are read 
as character. I'm sure it can be improved.  It's on GitHub in case 
anyone would like to take the time to suggest improvements:



https://github.com/sbgraves237/Ecfun


  Hope this helps.
  Spencer Graves


On 11/20/21 4:13 PM, Avi Gross via R-help wrote:

This seems to be a topic that comes up periodically. The various ways in R
and other packages for reading in data often come with methods that simply
guess wrong or encounter one or more data items in a column that really do
not fit so fields may just by default become a more common denominator of
character or perhaps floating point.

There are ways that some such programs can be given a hint of what you
expect or even be supplied with a way to coerce them into what you want
while being read in. But realistically, often a more practical  method might
be to take the data.frame variety you read in and before using it for other
purposes, check it for validity and make any needed changes. Simplistic ones
might be to see how many columns were read in to see if it matches
expectations or generate an error. Or you may trim columns (or rows) that
are not wanted.

In that vein, are there existing functions available that will accept what
types you want one or more columns to be in and that validate if the current
type is something else and then convert if needed? I mean we have functions
like as.integer(df$x ) or more flexibly as(df$x, "integer") and you may
simply build on a set of those and create others to suit any special needs.

Of course a good method carefully checks the results before over-writing as
sometimes the result may not be the same length (as shown below) or may
violate some other ideas or rules:


as(c(NULL, NA, 3, 3.1, "3.1", list(1,2,"a")), "character")

[1] "NA"  "3"   "3.1" "3.1" "1"   "2"   "a"

So if you have dates in some format, or sometimes an unknown format, there
are ways, including some others have shown, to make them into some other
date format or even make multiple columns that together embody the format.

What people sometimes do is assume software is perfect and should do
anything they want. It is the other way around and the programmer or data
creator has some responsibilities to use the right software on the right
data and that may also mean sanity checks along the way to  see if the data
is what you expect or alter it to be what you need.


-Original Message-
From: R-help  On Behalf Of Philip Monk
Sent: Saturday, November 20, 2021 3:28 PM
To: Jeff Newmiller 
Cc: R-help Mailing List 
Subject: Re: [R] Date read correctly from CSV, then reformatted incorrectly
by R

Thanks, Jeff.

I follow what you're doing below, but know I need to read up on Date /
POSIXct.  Helpful direction!  :)

On Sat, 20 Nov 2021 at 18:41, Jeff Newmiller 
wrote:


Beat me to it! But it is also worth noting that once converted to Date or

POSIXct, timestamps should be treated as data without regard to how that
data is displayed. When you choose to output that data you will have options
as to the display format associated with the function you are using for
output.


My take:

dta <- read.table( text=
"Buffer28/10/201619/11/2016  31/12/201616/01/2017

05/03/2017

1002.437110889-8.696748953.2392998162.443183304

2.346743827

2002.524329899-7.6888620683.3868117342.680347706

2.253885237

3002.100784256-8.0598558353.1437865072.615152896

2.015645973

4001.985608385-10.67072062.8945727912.591925038

2.057913137

5001.824982163-9.1225197362.5603507272.372226799

1.995863839

", header=TRUE, check.names=FALSE, as.is=TRUE)

dta

library(dplyr)
library(tidyr)

dt_fmt <- "%d/%m/%Y"

dta_long <- (   dta
 %>% pivot_longer( cols = -Buffer
 , names_to = "dt_chr"
 , values_to = "LST"
 )
 %>% mutate( dt_date = as.Date( dt_chr, format = dt_fmt )
   , dt_POSIXct = as.POSIXct( dt_chr, format = dt_fmt,

tz = "Etc/GMT+8" )

   )
 )

dta_long

On November 20, 2021 10:01:56 AM PST, Andrew Simmons 

wrote:

The as.Date function for a character class argument will try reading
in two formats (%Y-%m-%d and %Y/%m/%d).


This does not look like the format you have provided, which is why it
doesn't work. Try something like:


x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
"05/03/2017") as.Date(x, format = "%

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-22 Thread Robert Knight
Richard, 

This response was awe-inspiring.  Thank you.

-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Sunday, November 21, 2021 8:55 PM
To: Philip Monk 
Cc: R Project Help 
Subject: Re: [R] Date read correctly from CSV, then reformatted incorrectly
by R

CSV data is very often strangely laid out.  For analysis, Buffer Date
Reading
100...  ...
100...  ...
and so on is more like what a data frame should be.  I get quite annoyed
when I finally manage to extract data from a government agency only to find
that my tax money has been spent on making it harder to access than it
needed to be.

(1) You do NOT need any additional library to convert dates.
?strptime is quite capable.

(2) Just because reshaping CAN be done in R doesn't mean it
SHOULD be.  Instead of reading data in as the wrong format
and then hacking it into shape every time, it makes sense
to convert the data once and only once, then load the
converted data.  It took just a couple of minutes to write

  (CSVDecoder read: 'transpose-in.csv') bindOwn: [:source |
(CSVEncoder write: 'transpose-out.csv') bindOwn: [:target |
  source next bind: [:header | "Label date-1 ... date-n"
target nextPut: {header first. 'Date'. 'Reading'}.
[source atEnd] whileFalse: [
  source next bind: [:group |
group with: header keysAndValuesDo: [:index :reading :date |
  1 < index ifTrue: [
(date subStrings: '/') bind: [:dmy |
  (dmy third,'-',dmy second,'-',dmy first) bind: [:iso |
target nextPut: {group first. iso.
reading}]]].

in another programming language, run it, and turn your example into
Buffer,Date,Reading
100,2016-10-28,2.437110889
100,2016-11-19,-8.69674895
100,2016-12-31,3.239299816
100,2017-01-16,2.443183304
100,2017-03-05,2.346743827
200,2016-10-28,2.524329899
200,2016-11-19,-7.688862068
...
   You could do the same kind of thing easily in Perl, Python, F#, ...
   Then just read the table in using
   read.csv("transpose-out.csv", colClasses = c("integer","Date","numeric"))
   and you're away laughing.

(3) Of course you can do the whole thing in base R.

h <- read.csv("transpose-in.csv", header=FALSE, nrows=1,
stringsAsFactors=FALSE)
d <- strptime(h[1,-1], format="%d/%m/%Y")
b <- read.csv("transpose-in.csv", header=FALSE, skip=1)
r <- expand.grid(Date=d, Buffer=b[,1])
r$Result <- as.vector(t(as.matrix(b[,-1])))

Lessons:
(A) You don't have to read a CSV file (or any other) all in one piece.
This pays off when the structure is irregular.
(B) You don't HAVE to accept or convert column names.
(C) strptime is your friend.
(D) expand.grid is particularly handy for "matrix form" CSV data.
(E) Someone who suggests doing something in another language because
it is easier can end up with egg on his face when doing the whole
thing in R turns out to be easier, simpler, and far more obvious.
(A) really is an important lesson.
(F) It's *amazing* what you can do in base R.  It is useful to
familiarise yourself with its capabilities before considering other
packages.  Compositional data?  Not in base R.  Correspondence
analysis?  Not in base R.  Data reshaping?  Very much there.

On Sun, 21 Nov 2021 at 06:09, Philip Monk  wrote:

> Hello,
>
> Simple but infuriating problem.
>
> Reading in CSV of data using :
>
> ```
> # CSV file has column headers with date of scene capture in format 
> dd/mm/ # check.names = FALSE averts R incorrectly processing dates 
> due to '/'
> data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> FALSE)
>
> # Converts data table from wide (many columns) to long (many rows) and 
> creates the new object 'data_long'
> # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain 
> monthly data covering 2 years (the header row being the date, and rows 
> 2-21 being a value for each buffer).
> # Column headers for columns 2:25 are mutated into a column called 
> 'Date', values for each buffer and each date into the column 'LST'
> data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date", 
> values_to = "LST")
>
> # Instructs R to treat the 'Date' column data as a date data_long$Date 
> <- as.Date(data_long$Date) ```
>
> Using str(data), I can see that R has correctly read the dates in the 
> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>
> Once changing the type to 'Date', however, the date is reconfigured.  
> For instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>
> I've tried ```data_long$Date <- as.Date(data_

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-21 Thread Richard O'Keefe
CSV data is very often strangely laid out.  For analysis,
Buffer Date Reading
100...  ...
100...  ...
and so on is more like what a data frame should be.  I get
quite annoyed when I finally manage to extract data from a
government agency only to find that my tax money has been
spent on making it harder to access than it needed to be.

(1) You do NOT need any additional library to convert dates.
?strptime is quite capable.

(2) Just because reshaping CAN be done in R doesn't mean it
SHOULD be.  Instead of reading data in as the wrong format
and then hacking it into shape every time, it makes sense
to convert the data once and only once, then load the
converted data.  It took just a couple of minutes to write

  (CSVDecoder read: 'transpose-in.csv') bindOwn: [:source |
(CSVEncoder write: 'transpose-out.csv') bindOwn: [:target |
  source next bind: [:header | "Label date-1 ... date-n"
target nextPut: {header first. 'Date'. 'Reading'}.
[source atEnd] whileFalse: [
  source next bind: [:group |
group with: header keysAndValuesDo: [:index :reading :date |
  1 < index ifTrue: [
(date subStrings: '/') bind: [:dmy |
  (dmy third,'-',dmy second,'-',dmy first) bind: [:iso |
target nextPut: {group first. iso.
reading}]]].

in another programming language, run it, and turn your example into
Buffer,Date,Reading
100,2016-10-28,2.437110889
100,2016-11-19,-8.69674895
100,2016-12-31,3.239299816
100,2017-01-16,2.443183304
100,2017-03-05,2.346743827
200,2016-10-28,2.524329899
200,2016-11-19,-7.688862068
...
   You could do the same kind of thing easily in Perl, Python, F#, ...
   Then just read the table in using
   read.csv("transpose-out.csv", colClasses = c("integer","Date","numeric"))
   and you're away laughing.

(3) Of course you can do the whole thing in base R.

h <- read.csv("transpose-in.csv", header=FALSE, nrows=1,
stringsAsFactors=FALSE)
d <- strptime(h[1,-1], format="%d/%m/%Y")
b <- read.csv("transpose-in.csv", header=FALSE, skip=1)
r <- expand.grid(Date=d, Buffer=b[,1])
r$Result <- as.vector(t(as.matrix(b[,-1])))

Lessons:
(A) You don't have to read a CSV file (or any other) all in one piece.
This pays off when the structure is irregular.
(B) You don't HAVE to accept or convert column names.
(C) strptime is your friend.
(D) expand.grid is particularly handy for "matrix form" CSV data.
(E) Someone who suggests doing something in another language because
it is easier can end up with egg on his face when doing the whole
thing in R turns out to be easier, simpler, and far more obvious.
(A) really is an important lesson.
(F) It's *amazing* what you can do in base R.  It is useful to
familiarise yourself with its capabilities before considering other
packages.  Compositional data?  Not in base R.  Correspondence
analysis?  Not in base R.  Data reshaping?  Very much there.

On Sun, 21 Nov 2021 at 06:09, Philip Monk  wrote:

> Hello,
>
> Simple but infuriating problem.
>
> Reading in CSV of data using :
>
> ```
> # CSV file has column headers with date of scene capture in format
> dd/mm/
> # check.names = FALSE averts R incorrectly processing dates due to '/'
> data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> FALSE)
>
> # Converts data table from wide (many columns) to long (many rows) and
> creates the new object 'data_long'
> # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly
> data covering 2 years (the header row being the date, and rows 2-21 being a
> value for each buffer).
> # Column headers for columns 2:25 are mutated into a column called 'Date',
> values for each buffer and each date into the column 'LST'
> data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> values_to = "LST")
>
> # Instructs R to treat the 'Date' column data as a date
> data_long$Date <- as.Date(data_long$Date)
> ```
>
> Using str(data), I can see that R has correctly read the dates in the
> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>
> Once changing the type to 'Date', however, the date is reconfigured.  For
> instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>
> I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> persists or I get ```NA```.
>
> How do I make R change Date from 'chr' to 'date' without it going wrong?
>
> Suggestions/hints/solutions would be most welcome.  :)
>
> Thanks for your time,
>
> Philip
>
> Part-time PhD Student (Environmental Science)
> Lancaster University, UK.
>
> ~
>
> I asked a question a few weeks ago and put together the answer I needed
> from the responses but didn't know how to say thanks on this list.  So,
> thanks 

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Avi Gross via R-help
This seems to be a topic that comes up periodically. The various ways in R
and other packages for reading in data often come with methods that simply
guess wrong or encounter one or more data items in a column that really do
not fit so fields may just by default become a more common denominator of
character or perhaps floating point.

There are ways that some such programs can be given a hint of what you
expect or even be supplied with a way to coerce them into what you want
while being read in. But realistically, often a more practical  method might
be to take the data.frame variety you read in and before using it for other
purposes, check it for validity and make any needed changes. Simplistic ones
might be to see how many columns were read in to see if it matches
expectations or generate an error. Or you may trim columns (or rows) that
are not wanted.

In that vein, are there existing functions available that will accept what
types you want one or more columns to be in and that validate if the current
type is something else and then convert if needed? I mean we have functions
like as.integer(df$x ) or more flexibly as(df$x, "integer") and you may
simply build on a set of those and create others to suit any special needs.

Of course a good method carefully checks the results before over-writing as
sometimes the result may not be the same length (as shown below) or may
violate some other ideas or rules:

> as(c(NULL, NA, 3, 3.1, "3.1", list(1,2,"a")), "character")
[1] "NA"  "3"   "3.1" "3.1" "1"   "2"   "a"  

So if you have dates in some format, or sometimes an unknown format, there
are ways, including some others have shown, to make them into some other
date format or even make multiple columns that together embody the format.

What people sometimes do is assume software is perfect and should do
anything they want. It is the other way around and the programmer or data
creator has some responsibilities to use the right software on the right
data and that may also mean sanity checks along the way to  see if the data
is what you expect or alter it to be what you need.


-Original Message-
From: R-help  On Behalf Of Philip Monk
Sent: Saturday, November 20, 2021 3:28 PM
To: Jeff Newmiller 
Cc: R-help Mailing List 
Subject: Re: [R] Date read correctly from CSV, then reformatted incorrectly
by R

Thanks, Jeff.

I follow what you're doing below, but know I need to read up on Date /
POSIXct.  Helpful direction!  :)

On Sat, 20 Nov 2021 at 18:41, Jeff Newmiller 
wrote:
>
> Beat me to it! But it is also worth noting that once converted to Date or
POSIXct, timestamps should be treated as data without regard to how that
data is displayed. When you choose to output that data you will have options
as to the display format associated with the function you are using for
output.
>
> My take:
>
> dta <- read.table( text=
> "Buffer28/10/201619/11/2016  31/12/201616/01/2017
05/03/2017
> 1002.437110889-8.696748953.2392998162.443183304
2.346743827
> 2002.524329899-7.6888620683.3868117342.680347706
2.253885237
> 3002.100784256-8.0598558353.1437865072.615152896
2.015645973
> 4001.985608385-10.67072062.8945727912.591925038
2.057913137
> 5001.824982163-9.1225197362.5603507272.372226799
1.995863839
> ", header=TRUE, check.names=FALSE, as.is=TRUE)
>
> dta
>
> library(dplyr)
> library(tidyr)
>
> dt_fmt <- "%d/%m/%Y"
>
> dta_long <- (   dta
> %>% pivot_longer( cols = -Buffer
> , names_to = "dt_chr"
> , values_to = "LST"
> )
> %>% mutate( dt_date = as.Date( dt_chr, format = dt_fmt )
>   , dt_POSIXct = as.POSIXct( dt_chr, format = dt_fmt,
tz = "Etc/GMT+8" )
>   )
> )
>
> dta_long
>
> On November 20, 2021 10:01:56 AM PST, Andrew Simmons 
wrote:
> >The as.Date function for a character class argument will try reading 
> >in two formats (%Y-%m-%d and %Y/%m/%d).
> >
> >
> >This does not look like the format you have provided, which is why it 
> >doesn't work. Try something like:
> >
> >
> >x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", 
> >"05/03/2017") as.Date(x, format = "%d/%m/%Y")
> >
> >
> >which produces this output:
> >
> >
> >> x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
> >"05/03/2017")
> >> as.Date(x, format = "%d/%m/%Y")
> 

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Philip Monk
I am.  Long day, poorly small children!

P

On Sat, 20 Nov 2021, 21:08 Bert Gunter,  wrote:

> "I also know that '/' is a special character in R (if that's the right
> term) "
>
> That is false. I think you are confusing "/" with "\", which is R's
> *escape* character.
>
> > cat("a/nb")
> a/nb
> > cat("a\nb")
> a
> b
>
> It gets confusing especially in regex's, because "\" is used in regex
> syntax also.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
> On Sat, Nov 20, 2021 at 12:23 PM Philip Monk  wrote:
> >
> > Thanks, Andrew.  I didn't realise as.Date *only* read two formats, I
> > think I was tripped up by using %y instead of %Y, though I also know
> > that '/' is a special character in R (if that's the right term) and as
> > such know there is special syntax to use (which I don't know).
> >
> > On Sat, 20 Nov 2021 at 18:02, Andrew Simmons  wrote:
> > >
> > > The as.Date function for a character class argument will try reading
> in two formats (%Y-%m-%d and %Y/%m/%d).
> > >
> > >
> > > This does not look like the format you have provided, which is why it
> doesn't work. Try something like:
> > >
> > >
> > > x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
> "05/03/2017")
> > > as.Date(x, format = "%d/%m/%Y")
> > >
> > >
> > > which produces this output:
> > >
> > >
> > > > x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
> "05/03/2017")
> > > > as.Date(x, format = "%d/%m/%Y")
> > > [1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
> > > >
> > >
> > >
> > > much better than before! I hope this helps
> > >
> > > On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:
> > >>
> > >> Thanks Eric & Jeff.
> > >>
> > >> I'll certainly read up on lubridate, and the posting guide (again)
> > >> (this should be in plain text).
> > >>
> > >> CSV extract below...
> > >>
> > >> Philip
> > >>
> > >> Buffer28/10/201619/11/201631/12/201616/01/2017
> 05/03/2017
> > >> 1002.437110889-8.696748953.2392998162.443183304
> 2.346743827
> > >> 2002.524329899-7.6888620683.3868117342.680347706
> 2.253885237
> > >> 3002.100784256-8.0598558353.1437865072.615152896
> 2.015645973
> > >> 4001.985608385-10.67072062.8945727912.591925038
> 2.057913137
> > >> 5001.824982163-9.1225197362.5603507272.372226799
> 1.995863839
> > >>
> > >>
> > >> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
> > >> >
> > >> > Hello,
> > >> >
> > >> > Simple but infuriating problem.
> > >> >
> > >> > Reading in CSV of data using :
> > >> >
> > >> > ```
> > >> > # CSV file has column headers with date of scene capture in format
> dd/mm/
> > >> > # check.names = FALSE averts R incorrectly processing dates due to
> '/'
> > >> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv",
> check.names = FALSE)
> > >> >
> > >> > # Converts data table from wide (many columns) to long (many rows)
> and creates the new object 'data_long'
> > >> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain
> monthly data covering 2 years (the header row being the date, and rows 2-21
> being a value for each buffer).
> > >> > # Column headers for columns 2:25 are mutated into a column called
> 'Date', values for each buffer and each date into the column 'LST'
> > >> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> values_to = "LST")
> > >> >
> > >> > # Instructs R to treat the 'Date' column data as a date
> > >> > data_long$Date <- as.Date(data_long$Date)
> > >> > ```
> > >> >
> > >> > Using str(data), I can see that R has correctly read the dates in
> the format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
> > >> >
> > >> > Once changing the type to 'Date', however, the date is
> reconfigured.  For instance, 15/01/2010 (15 January 2010), becomes
> 0015-01-20.
> > >> >
> > >> > I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> persists or I get ```NA```.
> > >> >
> > >> > How do I make R change Date from 'chr' to 'date' without it going
> wrong?
> > >> >
> > >> > Suggestions/hints/solutions would be most welcome.  :)
> > >> >
> > >> > Thanks for your time,
> > >> >
> > >> > Philip
> > >> >
> > >> > Part-time PhD Student (Environmental Science)
> > >> > Lancaster University, UK.
> > >> >
> > >> > ~
> > >> >
> > >> > I asked a question a few weeks ago and put together the answer I
> needed from the responses but didn't know how to say thanks on this list.
> So, thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
> > >>
> > >> __
> > >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >> https://stat.ethz.ch/mailman/listinfo/r-help
> > >> PLEASE do read the 

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Bert Gunter
"I also know that '/' is a special character in R (if that's the right term) "

That is false. I think you are confusing "/" with "\", which is R's
*escape* character.

> cat("a/nb")
a/nb
> cat("a\nb")
a
b

It gets confusing especially in regex's, because "\" is used in regex
syntax also.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, Nov 20, 2021 at 12:23 PM Philip Monk  wrote:
>
> Thanks, Andrew.  I didn't realise as.Date *only* read two formats, I
> think I was tripped up by using %y instead of %Y, though I also know
> that '/' is a special character in R (if that's the right term) and as
> such know there is special syntax to use (which I don't know).
>
> On Sat, 20 Nov 2021 at 18:02, Andrew Simmons  wrote:
> >
> > The as.Date function for a character class argument will try reading in two 
> > formats (%Y-%m-%d and %Y/%m/%d).
> >
> >
> > This does not look like the format you have provided, which is why it 
> > doesn't work. Try something like:
> >
> >
> > x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
> > as.Date(x, format = "%d/%m/%Y")
> >
> >
> > which produces this output:
> >
> >
> > > x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", 
> > > "05/03/2017")
> > > as.Date(x, format = "%d/%m/%Y")
> > [1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
> > >
> >
> >
> > much better than before! I hope this helps
> >
> > On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:
> >>
> >> Thanks Eric & Jeff.
> >>
> >> I'll certainly read up on lubridate, and the posting guide (again)
> >> (this should be in plain text).
> >>
> >> CSV extract below...
> >>
> >> Philip
> >>
> >> Buffer28/10/201619/11/201631/12/201616/01/2017
> >> 05/03/2017
> >> 1002.437110889-8.696748953.2392998162.443183304
> >> 2.346743827
> >> 2002.524329899-7.6888620683.3868117342.680347706
> >> 2.253885237
> >> 3002.100784256-8.0598558353.1437865072.615152896
> >> 2.015645973
> >> 4001.985608385-10.67072062.8945727912.591925038
> >> 2.057913137
> >> 5001.824982163-9.1225197362.5603507272.372226799
> >> 1.995863839
> >>
> >>
> >> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
> >> >
> >> > Hello,
> >> >
> >> > Simple but infuriating problem.
> >> >
> >> > Reading in CSV of data using :
> >> >
> >> > ```
> >> > # CSV file has column headers with date of scene capture in format 
> >> > dd/mm/
> >> > # check.names = FALSE averts R incorrectly processing dates due to '/'
> >> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names = 
> >> > FALSE)
> >> >
> >> > # Converts data table from wide (many columns) to long (many rows) and 
> >> > creates the new object 'data_long'
> >> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain 
> >> > monthly data covering 2 years (the header row being the date, and rows 
> >> > 2-21 being a value for each buffer).
> >> > # Column headers for columns 2:25 are mutated into a column called 
> >> > 'Date', values for each buffer and each date into the column 'LST'
> >> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date", 
> >> > values_to = "LST")
> >> >
> >> > # Instructs R to treat the 'Date' column data as a date
> >> > data_long$Date <- as.Date(data_long$Date)
> >> > ```
> >> >
> >> > Using str(data), I can see that R has correctly read the dates in the 
> >> > format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
> >> >
> >> > Once changing the type to 'Date', however, the date is reconfigured.  
> >> > For instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
> >> >
> >> > I've tried ```data_long$Date <- as.Date(data_long$Date, format = 
> >> > "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the 
> >> > error persists or I get ```NA```.
> >> >
> >> > How do I make R change Date from 'chr' to 'date' without it going wrong?
> >> >
> >> > Suggestions/hints/solutions would be most welcome.  :)
> >> >
> >> > Thanks for your time,
> >> >
> >> > Philip
> >> >
> >> > Part-time PhD Student (Environmental Science)
> >> > Lancaster University, UK.
> >> >
> >> > ~
> >> >
> >> > I asked a question a few weeks ago and put together the answer I needed 
> >> > from the responses but didn't know how to say thanks on this list.  So, 
> >> > thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
> >>
> >> __
> >> R-help@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.
>
> __
> 

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Philip Monk
Thanks, Jeff.

I follow what you're doing below, but know I need to read up on Date /
POSIXct.  Helpful direction!  :)

On Sat, 20 Nov 2021 at 18:41, Jeff Newmiller  wrote:
>
> Beat me to it! But it is also worth noting that once converted to Date or 
> POSIXct, timestamps should be treated as data without regard to how that data 
> is displayed. When you choose to output that data you will have options as to 
> the display format associated with the function you are using for output.
>
> My take:
>
> dta <- read.table( text=
> "Buffer28/10/201619/11/2016  31/12/201616/01/201705/03/2017
> 1002.437110889-8.696748953.2392998162.4431833042.346743827
> 2002.524329899-7.6888620683.3868117342.680347706
> 2.253885237
> 3002.100784256-8.0598558353.1437865072.615152896
> 2.015645973
> 4001.985608385-10.67072062.8945727912.5919250382.057913137
> 5001.824982163-9.1225197362.5603507272.372226799
> 1.995863839
> ", header=TRUE, check.names=FALSE, as.is=TRUE)
>
> dta
>
> library(dplyr)
> library(tidyr)
>
> dt_fmt <- "%d/%m/%Y"
>
> dta_long <- (   dta
> %>% pivot_longer( cols = -Buffer
> , names_to = "dt_chr"
> , values_to = "LST"
> )
> %>% mutate( dt_date = as.Date( dt_chr, format = dt_fmt )
>   , dt_POSIXct = as.POSIXct( dt_chr, format = dt_fmt, tz 
> = "Etc/GMT+8" )
>   )
> )
>
> dta_long
>
> On November 20, 2021 10:01:56 AM PST, Andrew Simmons  
> wrote:
> >The as.Date function for a character class argument will try reading in two
> >formats (%Y-%m-%d and %Y/%m/%d).
> >
> >
> >This does not look like the format you have provided, which is why it
> >doesn't work. Try something like:
> >
> >
> >x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
> >as.Date(x, format = "%d/%m/%Y")
> >
> >
> >which produces this output:
> >
> >
> >> x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
> >"05/03/2017")
> >> as.Date(x, format = "%d/%m/%Y")
> >[1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
> >>
> >
> >
> >much better than before! I hope this helps
> >
> >On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:
> >
> >> Thanks Eric & Jeff.
> >>
> >> I'll certainly read up on lubridate, and the posting guide (again)
> >> (this should be in plain text).
> >>
> >> CSV extract below...
> >>
> >> Philip
> >>
> >> Buffer28/10/201619/11/201631/12/201616/01/2017
> >> 05/03/2017
> >> 1002.437110889-8.696748953.2392998162.443183304
> >> 2.346743827
> >> 2002.524329899-7.6888620683.3868117342.680347706
> >> 2.253885237
> >> 3002.100784256-8.0598558353.1437865072.615152896
> >> 2.015645973
> >> 4001.985608385-10.67072062.8945727912.591925038
> >> 2.057913137
> >> 5001.824982163-9.1225197362.5603507272.372226799
> >> 1.995863839
> >>
> >>
> >> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
> >> >
> >> > Hello,
> >> >
> >> > Simple but infuriating problem.
> >> >
> >> > Reading in CSV of data using :
> >> >
> >> > ```
> >> > # CSV file has column headers with date of scene capture in format
> >> dd/mm/
> >> > # check.names = FALSE averts R incorrectly processing dates due to '/'
> >> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> >> FALSE)
> >> >
> >> > # Converts data table from wide (many columns) to long (many rows) and
> >> creates the new object 'data_long'
> >> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain
> >> monthly data covering 2 years (the header row being the date, and rows 2-21
> >> being a value for each buffer).
> >> > # Column headers for columns 2:25 are mutated into a column called
> >> 'Date', values for each buffer and each date into the column 'LST'
> >> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> >> values_to = "LST")
> >> >
> >> > # Instructs R to treat the 'Date' column data as a date
> >> > data_long$Date <- as.Date(data_long$Date)
> >> > ```
> >> >
> >> > Using str(data), I can see that R has correctly read the dates in the
> >> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
> >> >
> >> > Once changing the type to 'Date', however, the date is reconfigured.
> >> For instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
> >> >
> >> > I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> >> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> >> persists or I get ```NA```.
> >> >
> >> > How do I make R change Date from 'chr' to 'date' without it going wrong?
> >> >
> >> > Suggestions/hints/solutions would be most welcome.  :)
> >> >
> >> > Thanks for your time,
> >> >
> >> > Philip
> >> >
> >> > Part-time PhD Student (Environmental Science)
> >> > 

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Philip Monk
Thanks, Andrew.  I didn't realise as.Date *only* read two formats, I
think I was tripped up by using %y instead of %Y, though I also know
that '/' is a special character in R (if that's the right term) and as
such know there is special syntax to use (which I don't know).

On Sat, 20 Nov 2021 at 18:02, Andrew Simmons  wrote:
>
> The as.Date function for a character class argument will try reading in two 
> formats (%Y-%m-%d and %Y/%m/%d).
>
>
> This does not look like the format you have provided, which is why it doesn't 
> work. Try something like:
>
>
> x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
> as.Date(x, format = "%d/%m/%Y")
>
>
> which produces this output:
>
>
> > x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
> > as.Date(x, format = "%d/%m/%Y")
> [1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
> >
>
>
> much better than before! I hope this helps
>
> On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:
>>
>> Thanks Eric & Jeff.
>>
>> I'll certainly read up on lubridate, and the posting guide (again)
>> (this should be in plain text).
>>
>> CSV extract below...
>>
>> Philip
>>
>> Buffer28/10/201619/11/201631/12/201616/01/201705/03/2017
>> 1002.437110889-8.696748953.2392998162.443183304
>> 2.346743827
>> 2002.524329899-7.6888620683.3868117342.680347706
>> 2.253885237
>> 3002.100784256-8.0598558353.1437865072.615152896
>> 2.015645973
>> 4001.985608385-10.67072062.8945727912.591925038
>> 2.057913137
>> 5001.824982163-9.1225197362.5603507272.372226799
>> 1.995863839
>>
>>
>> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
>> >
>> > Hello,
>> >
>> > Simple but infuriating problem.
>> >
>> > Reading in CSV of data using :
>> >
>> > ```
>> > # CSV file has column headers with date of scene capture in format 
>> > dd/mm/
>> > # check.names = FALSE averts R incorrectly processing dates due to '/'
>> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names = 
>> > FALSE)
>> >
>> > # Converts data table from wide (many columns) to long (many rows) and 
>> > creates the new object 'data_long'
>> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly 
>> > data covering 2 years (the header row being the date, and rows 2-21 being 
>> > a value for each buffer).
>> > # Column headers for columns 2:25 are mutated into a column called 'Date', 
>> > values for each buffer and each date into the column 'LST'
>> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date", 
>> > values_to = "LST")
>> >
>> > # Instructs R to treat the 'Date' column data as a date
>> > data_long$Date <- as.Date(data_long$Date)
>> > ```
>> >
>> > Using str(data), I can see that R has correctly read the dates in the 
>> > format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>> >
>> > Once changing the type to 'Date', however, the date is reconfigured.  For 
>> > instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>> >
>> > I've tried ```data_long$Date <- as.Date(data_long$Date, format = 
>> > "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the 
>> > error persists or I get ```NA```.
>> >
>> > How do I make R change Date from 'chr' to 'date' without it going wrong?
>> >
>> > Suggestions/hints/solutions would be most welcome.  :)
>> >
>> > Thanks for your time,
>> >
>> > Philip
>> >
>> > Part-time PhD Student (Environmental Science)
>> > Lancaster University, UK.
>> >
>> > ~
>> >
>> > I asked a question a few weeks ago and put together the answer I needed 
>> > from the responses but didn't know how to say thanks on this list.  So, 
>> > thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>>
>> __
>> R-help@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.

__
R-help@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.


Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Jeff Newmiller
Beat me to it! But it is also worth noting that once converted to Date or 
POSIXct, timestamps should be treated as data without regard to how that data 
is displayed. When you choose to output that data you will have options as to 
the display format associated with the function you are using for output.

My take:

dta <- read.table( text=
"Buffer28/10/201619/11/2016  31/12/201616/01/201705/03/2017
1002.437110889-8.696748953.2392998162.4431833042.346743827
2002.524329899-7.6888620683.3868117342.6803477062.253885237
3002.100784256-8.0598558353.1437865072.6151528962.015645973
4001.985608385-10.67072062.8945727912.5919250382.057913137
5001.824982163-9.1225197362.5603507272.3722267991.995863839
", header=TRUE, check.names=FALSE, as.is=TRUE)

dta

library(dplyr)
library(tidyr)

dt_fmt <- "%d/%m/%Y"

dta_long <- (   dta
%>% pivot_longer( cols = -Buffer
, names_to = "dt_chr"
, values_to = "LST"
)
%>% mutate( dt_date = as.Date( dt_chr, format = dt_fmt )
  , dt_POSIXct = as.POSIXct( dt_chr, format = dt_fmt, tz = 
"Etc/GMT+8" )
  )
)

dta_long

On November 20, 2021 10:01:56 AM PST, Andrew Simmons  wrote:
>The as.Date function for a character class argument will try reading in two
>formats (%Y-%m-%d and %Y/%m/%d).
>
>
>This does not look like the format you have provided, which is why it
>doesn't work. Try something like:
>
>
>x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
>as.Date(x, format = "%d/%m/%Y")
>
>
>which produces this output:
>
>
>> x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
>"05/03/2017")
>> as.Date(x, format = "%d/%m/%Y")
>[1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
>>
>
>
>much better than before! I hope this helps
>
>On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:
>
>> Thanks Eric & Jeff.
>>
>> I'll certainly read up on lubridate, and the posting guide (again)
>> (this should be in plain text).
>>
>> CSV extract below...
>>
>> Philip
>>
>> Buffer28/10/201619/11/201631/12/201616/01/2017
>> 05/03/2017
>> 1002.437110889-8.696748953.2392998162.443183304
>> 2.346743827
>> 2002.524329899-7.6888620683.3868117342.680347706
>> 2.253885237
>> 3002.100784256-8.0598558353.1437865072.615152896
>> 2.015645973
>> 4001.985608385-10.67072062.8945727912.591925038
>> 2.057913137
>> 5001.824982163-9.1225197362.5603507272.372226799
>> 1.995863839
>>
>>
>> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
>> >
>> > Hello,
>> >
>> > Simple but infuriating problem.
>> >
>> > Reading in CSV of data using :
>> >
>> > ```
>> > # CSV file has column headers with date of scene capture in format
>> dd/mm/
>> > # check.names = FALSE averts R incorrectly processing dates due to '/'
>> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
>> FALSE)
>> >
>> > # Converts data table from wide (many columns) to long (many rows) and
>> creates the new object 'data_long'
>> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain
>> monthly data covering 2 years (the header row being the date, and rows 2-21
>> being a value for each buffer).
>> > # Column headers for columns 2:25 are mutated into a column called
>> 'Date', values for each buffer and each date into the column 'LST'
>> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
>> values_to = "LST")
>> >
>> > # Instructs R to treat the 'Date' column data as a date
>> > data_long$Date <- as.Date(data_long$Date)
>> > ```
>> >
>> > Using str(data), I can see that R has correctly read the dates in the
>> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>> >
>> > Once changing the type to 'Date', however, the date is reconfigured.
>> For instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>> >
>> > I've tried ```data_long$Date <- as.Date(data_long$Date, format =
>> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
>> persists or I get ```NA```.
>> >
>> > How do I make R change Date from 'chr' to 'date' without it going wrong?
>> >
>> > Suggestions/hints/solutions would be most welcome.  :)
>> >
>> > Thanks for your time,
>> >
>> > Philip
>> >
>> > Part-time PhD Student (Environmental Science)
>> > Lancaster University, UK.
>> >
>> > ~
>> >
>> > I asked a question a few weeks ago and put together the answer I needed
>> from the responses but didn't know how to say thanks on this list.  So,
>> thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> 

Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Andrew Simmons
The as.Date function for a character class argument will try reading in two
formats (%Y-%m-%d and %Y/%m/%d).


This does not look like the format you have provided, which is why it
doesn't work. Try something like:


x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016", "05/03/2017")
as.Date(x, format = "%d/%m/%Y")


which produces this output:


> x <- c("28/10/2016", "19/11/2016", "31/12/2016", "16/01/2016",
"05/03/2017")
> as.Date(x, format = "%d/%m/%Y")
[1] "2016-10-28" "2016-11-19" "2016-12-31" "2016-01-16" "2017-03-05"
>


much better than before! I hope this helps

On Sat, Nov 20, 2021 at 12:49 PM Philip Monk  wrote:

> Thanks Eric & Jeff.
>
> I'll certainly read up on lubridate, and the posting guide (again)
> (this should be in plain text).
>
> CSV extract below...
>
> Philip
>
> Buffer28/10/201619/11/201631/12/201616/01/2017
> 05/03/2017
> 1002.437110889-8.696748953.2392998162.443183304
> 2.346743827
> 2002.524329899-7.6888620683.3868117342.680347706
> 2.253885237
> 3002.100784256-8.0598558353.1437865072.615152896
> 2.015645973
> 4001.985608385-10.67072062.8945727912.591925038
> 2.057913137
> 5001.824982163-9.1225197362.5603507272.372226799
> 1.995863839
>
>
> On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
> >
> > Hello,
> >
> > Simple but infuriating problem.
> >
> > Reading in CSV of data using :
> >
> > ```
> > # CSV file has column headers with date of scene capture in format
> dd/mm/
> > # check.names = FALSE averts R incorrectly processing dates due to '/'
> > data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> FALSE)
> >
> > # Converts data table from wide (many columns) to long (many rows) and
> creates the new object 'data_long'
> > # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain
> monthly data covering 2 years (the header row being the date, and rows 2-21
> being a value for each buffer).
> > # Column headers for columns 2:25 are mutated into a column called
> 'Date', values for each buffer and each date into the column 'LST'
> > data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> values_to = "LST")
> >
> > # Instructs R to treat the 'Date' column data as a date
> > data_long$Date <- as.Date(data_long$Date)
> > ```
> >
> > Using str(data), I can see that R has correctly read the dates in the
> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
> >
> > Once changing the type to 'Date', however, the date is reconfigured.
> For instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
> >
> > I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> persists or I get ```NA```.
> >
> > How do I make R change Date from 'chr' to 'date' without it going wrong?
> >
> > Suggestions/hints/solutions would be most welcome.  :)
> >
> > Thanks for your time,
> >
> > Philip
> >
> > Part-time PhD Student (Environmental Science)
> > Lancaster University, UK.
> >
> > ~
> >
> > I asked a question a few weeks ago and put together the answer I needed
> from the responses but didn't know how to say thanks on this list.  So,
> thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Philip Monk
Thanks Eric & Jeff.

I'll certainly read up on lubridate, and the posting guide (again)
(this should be in plain text).

CSV extract below...

Philip

Buffer28/10/201619/11/201631/12/201616/01/201705/03/2017
1002.437110889-8.696748953.2392998162.4431833042.346743827
2002.524329899-7.6888620683.3868117342.6803477062.253885237
3002.100784256-8.0598558353.1437865072.6151528962.015645973
4001.985608385-10.67072062.8945727912.5919250382.057913137
5001.824982163-9.1225197362.5603507272.3722267991.995863839


On Sat, 20 Nov 2021 at 17:08, Philip Monk  wrote:
>
> Hello,
>
> Simple but infuriating problem.
>
> Reading in CSV of data using :
>
> ```
> # CSV file has column headers with date of scene capture in format dd/mm/
> # check.names = FALSE averts R incorrectly processing dates due to '/'
> data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names = FALSE)
>
> # Converts data table from wide (many columns) to long (many rows) and 
> creates the new object 'data_long'
> # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly 
> data covering 2 years (the header row being the date, and rows 2-21 being a 
> value for each buffer).
> # Column headers for columns 2:25 are mutated into a column called 'Date', 
> values for each buffer and each date into the column 'LST'
> data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date", values_to 
> = "LST")
>
> # Instructs R to treat the 'Date' column data as a date
> data_long$Date <- as.Date(data_long$Date)
> ```
>
> Using str(data), I can see that R has correctly read the dates in the format 
> %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>
> Once changing the type to 'Date', however, the date is reconfigured.  For 
> instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>
> I've tried ```data_long$Date <- as.Date(data_long$Date, format = 
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error 
> persists or I get ```NA```.
>
> How do I make R change Date from 'chr' to 'date' without it going wrong?
>
> Suggestions/hints/solutions would be most welcome.  :)
>
> Thanks for your time,
>
> Philip
>
> Part-time PhD Student (Environmental Science)
> Lancaster University, UK.
>
> ~
>
> I asked a question a few weeks ago and put together the answer I needed from 
> the responses but didn't know how to say thanks on this list.  So, thanks 
> Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!

__
R-help@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.


Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Jeff Newmiller
a) R data frames are column oriented. Do not fight this.

b) Data frame header names are character type. Period. Do not fight this.

It sounds like you need to reshape your data after you read it in. Provide the 
first five lines of your CSV file (or a reasonable facsimile if your data are 
confidential) and someone (me if I get to it first) can help make it more 
adapted to R.

Also, make sure you configure your email software to send plain text... 
formatted email gets damaged to varying degrees by the automatic stripping of 
formatting performed by the mailing list. And periodically read the Posting 
Guide for other advice.

On November 20, 2021 9:08:39 AM PST, Philip Monk  wrote:
>Hello,
>
>Simple but infuriating problem.
>
>Reading in CSV of data using :
>
>```
># CSV file has column headers with date of scene capture in format
>dd/mm/
># check.names = FALSE averts R incorrectly processing dates due to '/'
>data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
>FALSE)
>
># Converts data table from wide (many columns) to long (many rows) and
>creates the new object 'data_long'
># Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly
>data covering 2 years (the header row being the date, and rows 2-21 being a
>value for each buffer).
># Column headers for columns 2:25 are mutated into a column called 'Date',
>values for each buffer and each date into the column 'LST'
>data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
>values_to = "LST")
>
># Instructs R to treat the 'Date' column data as a date
>data_long$Date <- as.Date(data_long$Date)
>```
>
>Using str(data), I can see that R has correctly read the dates in the
>format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>
>Once changing the type to 'Date', however, the date is reconfigured.  For
>instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>
>I've tried ```data_long$Date <- as.Date(data_long$Date, format =
>"%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
>persists or I get ```NA```.
>
>How do I make R change Date from 'chr' to 'date' without it going wrong?
>
>Suggestions/hints/solutions would be most welcome.  :)
>
>Thanks for your time,
>
>Philip
>
>Part-time PhD Student (Environmental Science)
>Lancaster University, UK.
>
>~
>
>I asked a question a few weeks ago and put together the answer I needed
>from the responses but didn't know how to say thanks on this list.  So,
>thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@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.

__
R-help@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.


Re: [R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Eric Berger
Hi Philip,
This is a recurring question and there are many ways to do this.
My preference is to use the lubridate package.

library(lubridate)
a <- "15/01/2010"
b <- dmy(a)
b
# "2010-01-15"
class(b)
# [1] "Date"

HTH,
Eric


On Sat, Nov 20, 2021 at 7:09 PM Philip Monk  wrote:

> Hello,
>
> Simple but infuriating problem.
>
> Reading in CSV of data using :
>
> ```
> # CSV file has column headers with date of scene capture in format
> dd/mm/
> # check.names = FALSE averts R incorrectly processing dates due to '/'
> data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
> FALSE)
>
> # Converts data table from wide (many columns) to long (many rows) and
> creates the new object 'data_long'
> # Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly
> data covering 2 years (the header row being the date, and rows 2-21 being a
> value for each buffer).
> # Column headers for columns 2:25 are mutated into a column called 'Date',
> values for each buffer and each date into the column 'LST'
> data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
> values_to = "LST")
>
> # Instructs R to treat the 'Date' column data as a date
> data_long$Date <- as.Date(data_long$Date)
> ```
>
> Using str(data), I can see that R has correctly read the dates in the
> format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.
>
> Once changing the type to 'Date', however, the date is reconfigured.  For
> instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.
>
> I've tried ```data_long$Date <- as.Date(data_long$Date, format =
> "%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
> persists or I get ```NA```.
>
> How do I make R change Date from 'chr' to 'date' without it going wrong?
>
> Suggestions/hints/solutions would be most welcome.  :)
>
> Thanks for your time,
>
> Philip
>
> Part-time PhD Student (Environmental Science)
> Lancaster University, UK.
>
> ~
>
> I asked a question a few weeks ago and put together the answer I needed
> from the responses but didn't know how to say thanks on this list.  So,
> thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] Date read correctly from CSV, then reformatted incorrectly by R

2021-11-20 Thread Philip Monk
Hello,

Simple but infuriating problem.

Reading in CSV of data using :

```
# CSV file has column headers with date of scene capture in format
dd/mm/
# check.names = FALSE averts R incorrectly processing dates due to '/'
data <- read.csv("C:/R_data/Bungala (b2000) julian.csv", check.names =
FALSE)

# Converts data table from wide (many columns) to long (many rows) and
creates the new object 'data_long'
# Column 1 is the 'Buffer' number (100-2000), Columns 2-25 contain monthly
data covering 2 years (the header row being the date, and rows 2-21 being a
value for each buffer).
# Column headers for columns 2:25 are mutated into a column called 'Date',
values for each buffer and each date into the column 'LST'
data_long <- data %>% pivot_longer(cols = 2:25, names_to = "Date",
values_to = "LST")

# Instructs R to treat the 'Date' column data as a date
data_long$Date <- as.Date(data_long$Date)
```

Using str(data), I can see that R has correctly read the dates in the
format %d/%m/%y (e.g. 15/12/2015) though has the data type as chr.

Once changing the type to 'Date', however, the date is reconfigured.  For
instance, 15/01/2010 (15 January 2010), becomes 0015-01-20.

I've tried ```data_long$Date <- as.Date(data_long$Date, format =
"%d/%m.%y")```, and also ```tryformat c("%d/%m%y")```, but either the error
persists or I get ```NA```.

How do I make R change Date from 'chr' to 'date' without it going wrong?

Suggestions/hints/solutions would be most welcome.  :)

Thanks for your time,

Philip

Part-time PhD Student (Environmental Science)
Lancaster University, UK.

~

I asked a question a few weeks ago and put together the answer I needed
from the responses but didn't know how to say thanks on this list.  So,
thanks Andrew Simmons, Bert Gunter, Jeff Newmiller and Daniel Nordlund!

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date

2021-11-05 Thread Val
Thank you All.
The issue was not reading different file. I just mistyped the column
name, instead of typing My_date I typed mydate in the email.  The
problem is solved by using this
dat=read.csv("myfile.csv",stringsAsFactors=FALS)
suggested by Jim.

On Thu, Nov 4, 2021 at 7:58 PM Jeff Newmiller  wrote:
>
> Then you are looking at a different file... check your filenames. You have 
> imported the column as character, and R has not yet recognized that it is 
> supposed to be a date, so it can only show what it found.
>
> You will almost certainly find your error if you make a reproducible example.
>
> On November 4, 2021 5:30:22 PM PDT, Val  wrote:
> >Jeff,
> >
> >The date from y data file looks like as follow in the Linux environment,
> >My_date
> >2019-09-16
> >2021-02-21
> >2021-02-22
> >2017-10-11
> >2017-10-10
> >2018-11-11
> >2017-10-27
> >2017-10-30
> >2019-05-20
> >
> >On Thu, Nov 4, 2021 at 5:00 PM Jeff Newmiller  
> >wrote:
> >>
> >> You are claiming behavior that is not something R does, but is something 
> >> Excel does constantly.
> >>
> >> Compare what your data file looks like using a text editor with what R has 
> >> imported. Absolutely do not use a spreadsheet program to do this.
> >>
> >> On November 4, 2021 2:43:25 PM PDT, Val  wrote:
> >> >IHi All, l,
> >> >
> >> >I am  reading a csv file  and one of the columns is named as  "mydate"
> >> > with this form, 2019-09-16.
> >> >
> >> >I am reading this file as
> >> >
> >> >dat=read.csv("myfile.csv")
> >> > the structure of the data looks like as follow
> >> >
> >> >str(dat)
> >> >mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...
> >> >
> >> >Please note the format  has  changed from -mm-dd  to mm/dd/
> >> >When I tried to change this   as a Date using
> >> >
> >> >as.Date(as.Date(mydate, format="%m/%d/%Y" )
> >> >I am getting this error message
> >> >Error in charToDate(x) :
> >> >  characte string is not in a standard unambiguous format
> >> >
> >> >My question is,
> >> >1. how can I read the file as it is (i.e., without changing the date 
> >> >format) ?
> >> >2. why does R change the date format?
> >> >
> >> >Thank you,
> >> >
> >> >__
> >> >R-help@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.
>
> --
> Sent from my phone. Please excuse my brevity.

__
R-help@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.


Re: [R] Date

2021-11-04 Thread Jeff Newmiller
Then you are looking at a different file... check your filenames. You have 
imported the column as character, and R has not yet recognized that it is 
supposed to be a date, so it can only show what it found.

You will almost certainly find your error if you make a reproducible example.

On November 4, 2021 5:30:22 PM PDT, Val  wrote:
>Jeff,
>
>The date from y data file looks like as follow in the Linux environment,
>My_date
>2019-09-16
>2021-02-21
>2021-02-22
>2017-10-11
>2017-10-10
>2018-11-11
>2017-10-27
>2017-10-30
>2019-05-20
>
>On Thu, Nov 4, 2021 at 5:00 PM Jeff Newmiller  wrote:
>>
>> You are claiming behavior that is not something R does, but is something 
>> Excel does constantly.
>>
>> Compare what your data file looks like using a text editor with what R has 
>> imported. Absolutely do not use a spreadsheet program to do this.
>>
>> On November 4, 2021 2:43:25 PM PDT, Val  wrote:
>> >IHi All, l,
>> >
>> >I am  reading a csv file  and one of the columns is named as  "mydate"
>> > with this form, 2019-09-16.
>> >
>> >I am reading this file as
>> >
>> >dat=read.csv("myfile.csv")
>> > the structure of the data looks like as follow
>> >
>> >str(dat)
>> >mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...
>> >
>> >Please note the format  has  changed from -mm-dd  to mm/dd/
>> >When I tried to change this   as a Date using
>> >
>> >as.Date(as.Date(mydate, format="%m/%d/%Y" )
>> >I am getting this error message
>> >Error in charToDate(x) :
>> >  characte string is not in a standard unambiguous format
>> >
>> >My question is,
>> >1. how can I read the file as it is (i.e., without changing the date 
>> >format) ?
>> >2. why does R change the date format?
>> >
>> >Thank you,
>> >
>> >__
>> >R-help@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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@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.


Re: [R] Date

2021-11-04 Thread Val
Jeff,

The date from y data file looks like as follow in the Linux environment,
My_date
2019-09-16
2021-02-21
2021-02-22
2017-10-11
2017-10-10
2018-11-11
2017-10-27
2017-10-30
2019-05-20

On Thu, Nov 4, 2021 at 5:00 PM Jeff Newmiller  wrote:
>
> You are claiming behavior that is not something R does, but is something 
> Excel does constantly.
>
> Compare what your data file looks like using a text editor with what R has 
> imported. Absolutely do not use a spreadsheet program to do this.
>
> On November 4, 2021 2:43:25 PM PDT, Val  wrote:
> >IHi All, l,
> >
> >I am  reading a csv file  and one of the columns is named as  "mydate"
> > with this form, 2019-09-16.
> >
> >I am reading this file as
> >
> >dat=read.csv("myfile.csv")
> > the structure of the data looks like as follow
> >
> >str(dat)
> >mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...
> >
> >Please note the format  has  changed from -mm-dd  to mm/dd/
> >When I tried to change this   as a Date using
> >
> >as.Date(as.Date(mydate, format="%m/%d/%Y" )
> >I am getting this error message
> >Error in charToDate(x) :
> >  characte string is not in a standard unambiguous format
> >
> >My question is,
> >1. how can I read the file as it is (i.e., without changing the date format) 
> >?
> >2. why does R change the date format?
> >
> >Thank you,
> >
> >__
> >R-help@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.

__
R-help@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.


Re: [R] Date

2021-11-04 Thread Spencer Graves
	  My speculation is that Microsoft Excel recognized that as a date and 
saved it in the "mm/dd/" format you saw when reading it into R with 
dat=read.csv("myfile.csv").



	  "str" told you the format.  You can convert that from character to 
Date using as.Date(dat$mydate, '%m/%d/%Y'), as documented in 
help('as.Date').



NOTE: The error message, "character string is not in a standard 
unambiguous format" is almost appropriate:  In this case, it's clear 
that "09/16/2019" refers to month 09, day 16, and year 2019.  However, 
if it were "09/06/2019", we would not know if it were September 6 or 9 
June of 2019.  If it were  "09/06/08", we would have the added 
possibility with the year first, followed by month and day:  June 8, 
2009.  This ambiguity is resolved most forcefully by ISO 8601.



  Hope this helps.
  Spencer Graves


On 11/4/21 5:30 PM, PIKAL Petr wrote:

Hi

Not sure why the date format was changed but if I am correct R do not read 
dates as dates but as character vector. You need to transfer such columns to 
dates by asDate. The error is probably from your use two asDate commands.

Cheers
Petr
-Original Message-
From: R-help  On Behalf Of Val
Sent: Thursday, November 4, 2021 10:43 PM
To: r-help@R-project.org (r-help@r-project.org) 
Subject: [R] Date

IHi All, l,

I am  reading a csv file  and one of the columns is named as  "mydate"
  with this form, 2019-09-16.

I am reading this file as

dat=read.csv("myfile.csv")
  the structure of the data looks like as follow

str(dat)
mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...

Please note the format  has  changed from -mm-dd  to mm/dd/
When I tried to change this   as a Date using

as.Date(as.Date(mydate, format="%m/%d/%Y" )
I am getting this error message
 Error in charToDate(x) :
   characte string is not in a standard unambiguous format

My question is,
1. how can I read the file as it is (i.e., without changing the date format) ?
2. why does R change the date format?

Thank you,

__
R-help@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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních 
partnerů PRECHEZA a.s. jsou zveřejněny na: 
https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about 
processing and protection of business partner’s personal data are available on 
website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a 
podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: 
https://www.precheza.cz/01-dovetek/ | This email and any documents attached to 
it may be confidential and are subject to the legally binding disclaimer: 
https://www.precheza.cz/en/01-disclaimer/

__
R-help@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.



__
R-help@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.


Re: [R] Date

2021-11-04 Thread PIKAL Petr
Hi

Not sure why the date format was changed but if I am correct R do not read 
dates as dates but as character vector. You need to transfer such columns to 
dates by asDate. The error is probably from your use two asDate commands.

Cheers
Petr
-Original Message-
From: R-help  On Behalf Of Val
Sent: Thursday, November 4, 2021 10:43 PM
To: r-help@R-project.org (r-help@r-project.org) 
Subject: [R] Date

IHi All, l,

I am  reading a csv file  and one of the columns is named as  "mydate"
 with this form, 2019-09-16.

I am reading this file as

dat=read.csv("myfile.csv")
 the structure of the data looks like as follow

str(dat)
mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...

Please note the format  has  changed from -mm-dd  to mm/dd/
When I tried to change this   as a Date using

as.Date(as.Date(mydate, format="%m/%d/%Y" )
I am getting this error message
Error in charToDate(x) :
  characte string is not in a standard unambiguous format

My question is,
1. how can I read the file as it is (i.e., without changing the date format) ?
2. why does R change the date format?

Thank you,

__
R-help@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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních 
partnerů PRECHEZA a.s. jsou zveřejněny na: 
https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about 
processing and protection of business partner’s personal data are available on 
website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a 
podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: 
https://www.precheza.cz/01-dovetek/ | This email and any documents attached to 
it may be confidential and are subject to the legally binding disclaimer: 
https://www.precheza.cz/en/01-disclaimer/

__
R-help@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.


Re: [R] Date

2021-11-04 Thread Jim Lemon
Hi Val,
Try this:

dat=read.csv("myfile.csv",stringsAsFactors=FALSE)

However, the apparently silent conversion of format is a mystery to
me. The only time I have struck something like this was when exporting
dates from Excel some years ago, and there was a silent conversion to
mm/dd/ format if the dates were in dd/mm/ format. Could you
post some sample data?

Jim


On Fri, Nov 5, 2021 at 8:44 AM Val  wrote:
>
> IHi All, l,
>
> I am  reading a csv file  and one of the columns is named as  "mydate"
>  with this form, 2019-09-16.
>
> I am reading this file as
>
> dat=read.csv("myfile.csv")
>  the structure of the data looks like as follow
>
> str(dat)
> mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...
>
> Please note the format  has  changed from -mm-dd  to mm/dd/
> When I tried to change this   as a Date using
>
> as.Date(as.Date(mydate, format="%m/%d/%Y" )
> I am getting this error message
> Error in charToDate(x) :
>   characte string is not in a standard unambiguous format
>
> My question is,
> 1. how can I read the file as it is (i.e., without changing the date format) ?
> 2. why does R change the date format?
>
> Thank you,
>
> __
> R-help@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.

__
R-help@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.


Re: [R] Date

2021-11-04 Thread Jeff Newmiller
You are claiming behavior that is not something R does, but is something Excel 
does constantly.

Compare what your data file looks like using a text editor with what R has 
imported. Absolutely do not use a spreadsheet program to do this.

On November 4, 2021 2:43:25 PM PDT, Val  wrote:
>IHi All, l,
>
>I am  reading a csv file  and one of the columns is named as  "mydate"
> with this form, 2019-09-16.
>
>I am reading this file as
>
>dat=read.csv("myfile.csv")
> the structure of the data looks like as follow
>
>str(dat)
>mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...
>
>Please note the format  has  changed from -mm-dd  to mm/dd/
>When I tried to change this   as a Date using
>
>as.Date(as.Date(mydate, format="%m/%d/%Y" )
>I am getting this error message
>Error in charToDate(x) :
>  characte string is not in a standard unambiguous format
>
>My question is,
>1. how can I read the file as it is (i.e., without changing the date format) ?
>2. why does R change the date format?
>
>Thank you,
>
>__
>R-help@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.

__
R-help@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.


[R] Date

2021-11-04 Thread Val
IHi All, l,

I am  reading a csv file  and one of the columns is named as  "mydate"
 with this form, 2019-09-16.

I am reading this file as

dat=read.csv("myfile.csv")
 the structure of the data looks like as follow

str(dat)
mydate : chr  "09/16/2019" "02/21/2021" "02/22/2021" "10/11/2017" ...

Please note the format  has  changed from -mm-dd  to mm/dd/
When I tried to change this   as a Date using

as.Date(as.Date(mydate, format="%m/%d/%Y" )
I am getting this error message
Error in charToDate(x) :
  characte string is not in a standard unambiguous format

My question is,
1. how can I read the file as it is (i.e., without changing the date format) ?
2. why does R change the date format?

Thank you,

__
R-help@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.


Re: [R] Date Time, as.POSIXct used locale, strange plot behavior

2021-04-30 Thread Tilmann Faul
Dear Enrico,

Thanks a lot, that clarifies the topic for me.
Checking the numeric representation i was not aware of.

Best Regards
Tilmann

On 30.04.21 11:17, Enrico Schumann wrote:
> On Fri, 30 Apr 2021, Tilmann Faul writes:
> 
>> Dear Jeff,
>>
>> Thanks for your answer.
>> Sys.timezone()  gives
>> [1] "Europe/Berlin"
>> I tried "Europe/Berlin" as tz argument, giving the same result als using
>> "CEST" (Central European Summer Time).
>> It seems to me, that using as.POSIXct without tz argument defaults to tz
>> UTC and with tz argument, either "CEST" or "Europ/Berlin" uses the
>> European tz, regarding the plot.
>> Never the less i do not understand why all of them have the same time
>> printout on my system.
>>
>> as.POSIXct("2021-04-21 00:00:00", tz="CEST")
>> # [1] "2021-04-21 CEST"
>> as.POSIXct("2021-04-21 00:00:00", tz="Europ/Berlin")
>> # [1] "2021-04-21 Europ"
>> as.POSIXct("2021-04-21 00:00:00")
>> # [1] "2021-04-21 CEST"
>>
>> Can someone comment on that, please?
>>
>> best Regards
>> Tilmann
> 
> Timezone names in general are not portable (i.e. not
> safe to use).  You may always specify a timezone name,
> but it may be ignored:
> 
> as.POSIXct("2021-04-21 00:00:00", tz = "PartyTime")
> ## "2021-04-21 PartyTime"
> 
> So just because your system prints "CEST", it does not
> mean it has recognised it as a timezone. (To make it
> even more complicated: your system may display a time
> as "CEST", meaning a time in Central European Summer
> Time, but still not accept "CEST" as _input_ for a
> timezone name.)
> 
> 'POSIXct' represents time as a number (seconds since
> 1970). To compare the results of different calls, it is
> easier to compare those numbers.
> 
> dput(as.POSIXct("2021-04-21 00:00:00", tz="CEST"))
> ## structure(1618963200, class = c("POSIXct", "POSIXt"), tzone = "CEST")
> dput(as.POSIXct("2021-04-21 00:00:00", tz="PartyTime"))
> ## structure(1618963200, class = c("POSIXct", "POSIXt"), tzone = 
> "PartyTime")
> 
> dput(as.POSIXct("2021-04-21 00:00:00", tz="Europe/Berlin"))
> ## structure(1618956000, class = c("POSIXct", "POSIXt"), tzone = 
> "Europe/Berlin")
> dput(as.POSIXct("2021-04-21 00:00:00"))
> ## structure(1618956000, class = c("POSIXct", "POSIXt"), tzone = "")
> 
> So in your case, "CEST" is (most likely) not recognised
> as a valid input for a timezone name, and hence it is
> ignored, but still displayed.
> 
> HTH
> Enrico
> 
> 
>> On 29.04.21 23:19, Jeff Newmiller wrote:
>>> What is your TZ environment variable set to? That's what time conversion 
>>> defaults to  ?DateTimeClasses
>>>
>>> Also, I am not sure CEST is a valid timezone designation... it can be 
>>> system dependent, but using one of the elements listed in ?OlsonNames.
>>>
>>> On April 29, 2021 12:22:44 PM PDT, Tilmann Faul  
>>> wrote:
 Hy,

 stumbled over the following problem while plotting DateTime Objects.

 plot(as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59")), c(0,
 1), type='l')

 arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
   0.3,
   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
   0.2,
   length=0.07, angle=15)

 # arrow at 02:00, why?

 arrows(as.POSIXct("2021-04-21 00:00:00"),
   0.3,
   as.POSIXct("2021-04-21 00:00:00"),
   0.2,
   length=0.07, angle=15, col='red')

 # arrow at 00:00 as expected

 as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59"))[1]
 # [1] "2021-04-21 CEST"
 as.POSIXct("2021-04-21 00:00:00", tz="CEST")
 # [1] "2021-04-21 CEST"
 as.POSIXct("2021-04-21 00:00:00")
 # [1] "2021-04-21 CEST"

 all representations on my system are the same, why is the plot location
 of the arrows different??
 I am located in Germany, my locale:
 Sys.getlocale()
 [1]
 "LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C"

 Any Idea?

 Best regards
 Tilmann

 __
 R-help@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.
>>>
>>
>> __
>> R-help@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.
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help

Re: [R] Date Time, as.POSIXct used locale, strange plot behavior

2021-04-30 Thread Enrico Schumann
On Fri, 30 Apr 2021, Tilmann Faul writes:

> Dear Jeff,
>
> Thanks for your answer.
> Sys.timezone()  gives
> [1] "Europe/Berlin"
> I tried "Europe/Berlin" as tz argument, giving the same result als using
> "CEST" (Central European Summer Time).
> It seems to me, that using as.POSIXct without tz argument defaults to tz
> UTC and with tz argument, either "CEST" or "Europ/Berlin" uses the
> European tz, regarding the plot.
> Never the less i do not understand why all of them have the same time
> printout on my system.
>
> as.POSIXct("2021-04-21 00:00:00", tz="CEST")
> # [1] "2021-04-21 CEST"
> as.POSIXct("2021-04-21 00:00:00", tz="Europ/Berlin")
> # [1] "2021-04-21 Europ"
> as.POSIXct("2021-04-21 00:00:00")
> # [1] "2021-04-21 CEST"
>
> Can someone comment on that, please?
>
> best Regards
> Tilmann

Timezone names in general are not portable (i.e. not
safe to use).  You may always specify a timezone name,
but it may be ignored:

as.POSIXct("2021-04-21 00:00:00", tz = "PartyTime")
## "2021-04-21 PartyTime"

So just because your system prints "CEST", it does not
mean it has recognised it as a timezone. (To make it
even more complicated: your system may display a time
as "CEST", meaning a time in Central European Summer
Time, but still not accept "CEST" as _input_ for a
timezone name.)

'POSIXct' represents time as a number (seconds since
1970). To compare the results of different calls, it is
easier to compare those numbers.

dput(as.POSIXct("2021-04-21 00:00:00", tz="CEST"))
## structure(1618963200, class = c("POSIXct", "POSIXt"), tzone = "CEST")
dput(as.POSIXct("2021-04-21 00:00:00", tz="PartyTime"))
## structure(1618963200, class = c("POSIXct", "POSIXt"), tzone = 
"PartyTime")

dput(as.POSIXct("2021-04-21 00:00:00", tz="Europe/Berlin"))
## structure(1618956000, class = c("POSIXct", "POSIXt"), tzone = 
"Europe/Berlin")
dput(as.POSIXct("2021-04-21 00:00:00"))
## structure(1618956000, class = c("POSIXct", "POSIXt"), tzone = "")

So in your case, "CEST" is (most likely) not recognised
as a valid input for a timezone name, and hence it is
ignored, but still displayed.

HTH
Enrico


> On 29.04.21 23:19, Jeff Newmiller wrote:
>> What is your TZ environment variable set to? That's what time conversion 
>> defaults to  ?DateTimeClasses
>> 
>> Also, I am not sure CEST is a valid timezone designation... it can be system 
>> dependent, but using one of the elements listed in ?OlsonNames.
>> 
>> On April 29, 2021 12:22:44 PM PDT, Tilmann Faul  
>> wrote:
>>> Hy,
>>>
>>> stumbled over the following problem while plotting DateTime Objects.
>>>
>>> plot(as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59")), c(0,
>>> 1), type='l')
>>>
>>> arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
>>>   0.3,
>>>   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
>>>   0.2,
>>>   length=0.07, angle=15)
>>>
>>> # arrow at 02:00, why?
>>>
>>> arrows(as.POSIXct("2021-04-21 00:00:00"),
>>>   0.3,
>>>   as.POSIXct("2021-04-21 00:00:00"),
>>>   0.2,
>>>   length=0.07, angle=15, col='red')
>>>
>>> # arrow at 00:00 as expected
>>>
>>> as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59"))[1]
>>> # [1] "2021-04-21 CEST"
>>> as.POSIXct("2021-04-21 00:00:00", tz="CEST")
>>> # [1] "2021-04-21 CEST"
>>> as.POSIXct("2021-04-21 00:00:00")
>>> # [1] "2021-04-21 CEST"
>>>
>>> all representations on my system are the same, why is the plot location
>>> of the arrows different??
>>> I am located in Germany, my locale:
>>> Sys.getlocale()
>>> [1]
>>> "LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C"
>>>
>>> Any Idea?
>>>
>>> Best regards
>>> Tilmann
>>>
>>> __
>>> R-help@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.
>>
>
> __
> R-help@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.

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-help@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.


Re: [R] Date Time, as.POSIXct used locale, strange plot behavior

2021-04-30 Thread Tilmann Faul
Dear Jeff,

Thanks for your answer.
Sys.timezone()  gives
[1] "Europe/Berlin"
I tried "Europe/Berlin" as tz argument, giving the same result als using
"CEST" (Central European Summer Time).
It seems to me, that using as.POSIXct without tz argument defaults to tz
UTC and with tz argument, either "CEST" or "Europ/Berlin" uses the
European tz, regarding the plot.
Never the less i do not understand why all of them have the same time
printout on my system.

as.POSIXct("2021-04-21 00:00:00", tz="CEST")
# [1] "2021-04-21 CEST"
as.POSIXct("2021-04-21 00:00:00", tz="Europ/Berlin")
# [1] "2021-04-21 Europ"
as.POSIXct("2021-04-21 00:00:00")
# [1] "2021-04-21 CEST"

Can someone comment on that, please?

best Regards
Tilmann

On 29.04.21 23:19, Jeff Newmiller wrote:
> What is your TZ environment variable set to? That's what time conversion 
> defaults to  ?DateTimeClasses
> 
> Also, I am not sure CEST is a valid timezone designation... it can be system 
> dependent, but using one of the elements listed in ?OlsonNames.
> 
> On April 29, 2021 12:22:44 PM PDT, Tilmann Faul  
> wrote:
>> Hy,
>>
>> stumbled over the following problem while plotting DateTime Objects.
>>
>> plot(as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59")), c(0,
>> 1), type='l')
>>
>> arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
>>   0.3,
>>   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
>>   0.2,
>>   length=0.07, angle=15)
>>
>> # arrow at 02:00, why?
>>
>> arrows(as.POSIXct("2021-04-21 00:00:00"),
>>   0.3,
>>   as.POSIXct("2021-04-21 00:00:00"),
>>   0.2,
>>   length=0.07, angle=15, col='red')
>>
>> # arrow at 00:00 as expected
>>
>> as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59"))[1]
>> # [1] "2021-04-21 CEST"
>> as.POSIXct("2021-04-21 00:00:00", tz="CEST")
>> # [1] "2021-04-21 CEST"
>> as.POSIXct("2021-04-21 00:00:00")
>> # [1] "2021-04-21 CEST"
>>
>> all representations on my system are the same, why is the plot location
>> of the arrows different??
>> I am located in Germany, my locale:
>> Sys.getlocale()
>> [1]
>> "LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C"
>>
>> Any Idea?
>>
>> Best regards
>> Tilmann
>>
>> __
>> R-help@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.
>

__
R-help@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.


Re: [R] Date Time, as.POSIXct used locale, strange plot behavior

2021-04-30 Thread PIKAL Petr
Hi.

It should be probably CET not CEST.

arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
   0.3,
   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
   0.2,
   length=0.07, angle=15)


arrows(as.POSIXct("2021-04-21 00:00:00", tz="CET"),
   0.3,
   as.POSIXct("2021-04-21 00:00:00", tz="CET"),
   0.2,
   length=0.07, angle=15)

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Jeff Newmiller
> Sent: Thursday, April 29, 2021 11:20 PM
> To: r-help@r-project.org; Tilmann Faul 
> Subject: Re: [R] Date Time, as.POSIXct used locale, strange plot behavior
> 
> What is your TZ environment variable set to? That's what time conversion
> defaults to  ?DateTimeClasses
> 
> Also, I am not sure CEST is a valid timezone designation... it can be
system
> dependent, but using one of the elements listed in ?OlsonNames.
> 
> On April 29, 2021 12:22:44 PM PDT, Tilmann Faul 
> wrote:
> >Hy,
> >
> >stumbled over the following problem while plotting DateTime Objects.
> >
> >plot(as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59")), c(0,
> >1), type='l')
> >
> >arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
> >   0.3,
> >   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
> >   0.2,
> >   length=0.07, angle=15)
> >
> ># arrow at 02:00, why?
> >
> >arrows(as.POSIXct("2021-04-21 00:00:00"),
> >   0.3,
> >   as.POSIXct("2021-04-21 00:00:00"),
> >   0.2,
> >   length=0.07, angle=15, col='red')
> >
> ># arrow at 00:00 as expected
> >
> >as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59"))[1] # [1]
> >"2021-04-21 CEST"
> >as.POSIXct("2021-04-21 00:00:00", tz="CEST") # [1] "2021-04-21 CEST"
> >as.POSIXct("2021-04-21 00:00:00")
> ># [1] "2021-04-21 CEST"
> >
> >all representations on my system are the same, why is the plot location
> >of the arrows different??
> >I am located in Germany, my locale:
> >Sys.getlocale()
> >[1]
> >"LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-
> 8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-
> 8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-
> 8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_D
> E.UTF-8;LC_IDENTIFICATION=C"
> >
> >Any Idea?
> >
> >Best regards
> >Tilmann
> >
> >__
> >R-help@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.
> 
> __
> R-help@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.
__
R-help@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.


Re: [R] Date Time, as.POSIXct used locale, strange plot behavior

2021-04-29 Thread Jeff Newmiller
What is your TZ environment variable set to? That's what time conversion 
defaults to  ?DateTimeClasses

Also, I am not sure CEST is a valid timezone designation... it can be system 
dependent, but using one of the elements listed in ?OlsonNames.

On April 29, 2021 12:22:44 PM PDT, Tilmann Faul  
wrote:
>Hy,
>
>stumbled over the following problem while plotting DateTime Objects.
>
>plot(as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59")), c(0,
>1), type='l')
>
>arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
>   0.3,
>   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
>   0.2,
>   length=0.07, angle=15)
>
># arrow at 02:00, why?
>
>arrows(as.POSIXct("2021-04-21 00:00:00"),
>   0.3,
>   as.POSIXct("2021-04-21 00:00:00"),
>   0.2,
>   length=0.07, angle=15, col='red')
>
># arrow at 00:00 as expected
>
>as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59"))[1]
># [1] "2021-04-21 CEST"
>as.POSIXct("2021-04-21 00:00:00", tz="CEST")
># [1] "2021-04-21 CEST"
>as.POSIXct("2021-04-21 00:00:00")
># [1] "2021-04-21 CEST"
>
>all representations on my system are the same, why is the plot location
>of the arrows different??
>I am located in Germany, my locale:
>Sys.getlocale()
>[1]
>"LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C"
>
>Any Idea?
>
>Best regards
>Tilmann
>
>__
>R-help@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.

__
R-help@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.


[R] Date Time, as.POSIXct used locale, strange plot behavior

2021-04-29 Thread Tilmann Faul
Hy,

stumbled over the following problem while plotting DateTime Objects.

plot(as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59")), c(0,
1), type='l')

arrows(as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
   0.3,
   as.POSIXct("2021-04-21 00:00:00", tz="CEST"),
   0.2,
   length=0.07, angle=15)

# arrow at 02:00, why?

arrows(as.POSIXct("2021-04-21 00:00:00"),
   0.3,
   as.POSIXct("2021-04-21 00:00:00"),
   0.2,
   length=0.07, angle=15, col='red')

# arrow at 00:00 as expected

as.POSIXct(c("2021-04-21 00:00:00", "2021-04-21 23:59:59"))[1]
# [1] "2021-04-21 CEST"
as.POSIXct("2021-04-21 00:00:00", tz="CEST")
# [1] "2021-04-21 CEST"
as.POSIXct("2021-04-21 00:00:00")
# [1] "2021-04-21 CEST"

all representations on my system are the same, why is the plot location
of the arrows different??
I am located in Germany, my locale:
Sys.getlocale()
[1]
"LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C"

Any Idea?

Best regards
Tilmann

__
R-help@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.


Re: [R] date

2020-09-21 Thread Duncan Murdoch
Another way to do this is to use the xtfrm() function.  That function 
creates numerical values from many different starting types, so you can 
just change the sign to change the sort order:


df[order(df$ID, -xtfrm(df$date2)),]

I never did figure out where the name came from.

Duncan Murdoch

On 21/09/2020 3:41 p.m., Sarah Goslee wrote:

Hi,

Nice reproducible example.

rev(df$date2) isn't doing what you think it's doing - try looking at
it by itself.

Some digging into ?order will get you what you are after:

df[order(df$ID, df$date2, decreasing=c(FALSE, TRUE), method="radix"),]


df[order(df$ID, df$date2, decreasing=c(FALSE, TRUE), method="radix"),]

   ID  date2
2 A1 2005-01-27
1 A1 2004-09-17
3 A1 2003-05-07
4 A2 2017-05-21
5 A2 2016-09-12
6 A3 2013-01-25
7 A4 2019-09-27

Sarah

On Mon, Sep 21, 2020 at 2:41 PM Val  wrote:


Hi All,

I am trying to sort dates within a group. My sample data is

df <-read.table(text="ID date
A1   09/17/04
A1   01/27/05
A1   05/07/03
A2   05/21/17
A2   09/12/16
A3   01/25/13
A4   09/27/19",header=TRUE,stringsAsFactors=F)
df$date2 = as.Date(strptime(df$date,format="%m/%d/%y"))
df$date =NULL

I want to sort  date2  from recent to oldest.  within the ID group and
I used this,
df <- df[order(df$ID, rev((df$date2))),]. It did not work and teh
output is  shown below.

ID  date2
2 A1 2005-01-27
3 A1 2003-05-07
1 A1 2004-09-17
5 A2 2016-09-12
4 A2 2017-05-21
6 A3 2013-01-25
7 A4 2019-09-27
What am I missing?
Thank you.

__
R-help@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.






__
R-help@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.


Re: [R] date

2020-09-21 Thread Sarah Goslee
Hi,

Nice reproducible example.

rev(df$date2) isn't doing what you think it's doing - try looking at
it by itself.

Some digging into ?order will get you what you are after:

df[order(df$ID, df$date2, decreasing=c(FALSE, TRUE), method="radix"),]

> df[order(df$ID, df$date2, decreasing=c(FALSE, TRUE), method="radix"),]
  ID  date2
2 A1 2005-01-27
1 A1 2004-09-17
3 A1 2003-05-07
4 A2 2017-05-21
5 A2 2016-09-12
6 A3 2013-01-25
7 A4 2019-09-27

Sarah

On Mon, Sep 21, 2020 at 2:41 PM Val  wrote:
>
> Hi All,
>
> I am trying to sort dates within a group. My sample data is
>
> df <-read.table(text="ID date
> A1   09/17/04
> A1   01/27/05
> A1   05/07/03
> A2   05/21/17
> A2   09/12/16
> A3   01/25/13
> A4   09/27/19",header=TRUE,stringsAsFactors=F)
> df$date2 = as.Date(strptime(df$date,format="%m/%d/%y"))
> df$date =NULL
>
> I want to sort  date2  from recent to oldest.  within the ID group and
> I used this,
> df <- df[order(df$ID, rev((df$date2))),]. It did not work and teh
> output is  shown below.
>
> ID  date2
> 2 A1 2005-01-27
> 3 A1 2003-05-07
> 1 A1 2004-09-17
> 5 A2 2016-09-12
> 4 A2 2017-05-21
> 6 A3 2013-01-25
> 7 A4 2019-09-27
> What am I missing?
> Thank you.
>
> __
> R-help@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.



-- 
Sarah Goslee (she/her)
http://www.numberwright.com

__
R-help@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.


[R] date

2020-09-21 Thread Val
Hi All,

I am trying to sort dates within a group. My sample data is

df <-read.table(text="ID date
A1   09/17/04
A1   01/27/05
A1   05/07/03
A2   05/21/17
A2   09/12/16
A3   01/25/13
A4   09/27/19",header=TRUE,stringsAsFactors=F)
df$date2 = as.Date(strptime(df$date,format="%m/%d/%y"))
df$date =NULL

I want to sort  date2  from recent to oldest.  within the ID group and
I used this,
df <- df[order(df$ID, rev((df$date2))),]. It did not work and teh
output is  shown below.

ID  date2
2 A1 2005-01-27
3 A1 2003-05-07
1 A1 2004-09-17
5 A2 2016-09-12
4 A2 2017-05-21
6 A3 2013-01-25
7 A4 2019-09-27
What am I missing?
Thank you.

__
R-help@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.


Re: [R] date conversion problem

2020-08-13 Thread Abdoulaye Sarr
Hi Jim,

Thanks for the hint, that makes sense and I'll arrange accordingly.
Best regards,
Abdoulaye

On Thu, Aug 13, 2020 at 8:38 AM Jim Lemon  wrote:

> Hi Abdoulaye,
> It looks to me as though your offsets are in hours, not days. You can
> get a rough date like this:
>
> time<-c(1569072,1569096,1569120,1569144,
>  1569168,1569192,1569216,1569240)
> time_d<-as.Date("1800-01-01")+time/24
> time_d
> [1] "1979-01-01" "1979-01-02" "1979-01-03" "1979-01-04" "1979-01-05"
> [6] "1979-01-06" "1979-01-07" "1979-01-08"
>
> Jim
>
> On Thu, Aug 13, 2020 at 6:10 PM Abdoulaye Sarr 
> wrote:
> >
> > I have dataset with time sine 1800-01-01 and extracted data from 1981 to
> > 2019 and used these lines for the data conversion:
> > > time_d <- as.Date(time, format="%j", origin=as.Date("1800-01-01"))
> > > time_years <- format(time_d, "%Y")
> > > time_months <- format(time_d, "%m")
> > > time_year_months <- format(time_d, "%Y-%m")
> > > head(time_d)
> > [1] "6095-12-22" "6096-01-15" "6096-02-08" "6096-03-03" "6096-03-27"
> > "6096-04-20"
> >
> > As you see these gregorian dates are unrealistic and wonder what I am
> doing
> > wrong?
> > The time from the raw file in Jd are like this:
> >
> > > time
> >[1] 1569072 1569096 1569120 1569144 1569168 1569192 1569216
> 1569240etc.
> >
> > Hope hint and/or suggestion to solve this.
> >
> > Best regards
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date conversion problem

2020-08-13 Thread Jim Lemon
Hi Abdoulaye,
It looks to me as though your offsets are in hours, not days. You can
get a rough date like this:

time<-c(1569072,1569096,1569120,1569144,
 1569168,1569192,1569216,1569240)
time_d<-as.Date("1800-01-01")+time/24
time_d
[1] "1979-01-01" "1979-01-02" "1979-01-03" "1979-01-04" "1979-01-05"
[6] "1979-01-06" "1979-01-07" "1979-01-08"

Jim

On Thu, Aug 13, 2020 at 6:10 PM Abdoulaye Sarr  wrote:
>
> I have dataset with time sine 1800-01-01 and extracted data from 1981 to
> 2019 and used these lines for the data conversion:
> > time_d <- as.Date(time, format="%j", origin=as.Date("1800-01-01"))
> > time_years <- format(time_d, "%Y")
> > time_months <- format(time_d, "%m")
> > time_year_months <- format(time_d, "%Y-%m")
> > head(time_d)
> [1] "6095-12-22" "6096-01-15" "6096-02-08" "6096-03-03" "6096-03-27"
> "6096-04-20"
>
> As you see these gregorian dates are unrealistic and wonder what I am doing
> wrong?
> The time from the raw file in Jd are like this:
>
> > time
>[1] 1569072 1569096 1569120 1569144 1569168 1569192 1569216 1569240etc.
>
> Hope hint and/or suggestion to solve this.
>
> Best regards
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.


[R] date conversion problem

2020-08-13 Thread Abdoulaye Sarr
I have dataset with time sine 1800-01-01 and extracted data from 1981 to
2019 and used these lines for the data conversion:
> time_d <- as.Date(time, format="%j", origin=as.Date("1800-01-01"))
> time_years <- format(time_d, "%Y")
> time_months <- format(time_d, "%m")
> time_year_months <- format(time_d, "%Y-%m")
> head(time_d)
[1] "6095-12-22" "6096-01-15" "6096-02-08" "6096-03-03" "6096-03-27"
"6096-04-20"

As you see these gregorian dates are unrealistic and wonder what I am doing
wrong?
The time from the raw file in Jd are like this:

> time
   [1] 1569072 1569096 1569120 1569144 1569168 1569192 1569216 1569240etc.

Hope hint and/or suggestion to solve this.

Best regards

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date Conversion Problem

2020-08-12 Thread Eric Berger
nice

On Wed, Aug 12, 2020 at 6:18 PM Bert Gunter  wrote:

> Extra packages are not needed.
>
> My question is: why change the character representation at all?  See the
> format argument of ?as.Date.
>
> > as.Date("20010102",format="%Y%m%d")
> [1] "2001-01-02" ## the default format for the print method for Date
> objects
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, Aug 12, 2020 at 8:07 AM Eric Berger  wrote:
>
>> library(lubridate)
>> a <- "20200403"
>> lubridate::ymd(a)
>> # 2020-04-03
>>
>> HTH,
>> Eric
>>
>>
>> On Wed, Aug 12, 2020 at 5:57 PM Stephen P. Molnar > >
>> wrote:
>>
>> > i have written an R script which allow me to plot the number of Covid-10
>> > cases reported by he state of Ohio. In that se t of data the date format
>> > is in the form -mm-dd.
>> >
>> > My script uses:
>> >
>> > datebreaks <- seq(as.Date("2020-01-01"), as.Date("2020-08-10"), by="1
>> > week")
>> > .
>> > .
>> > .
>> >+ scale_x_date(breaks=datebreaks)
>> >+ theme(axis.text.x = element_text(angle=30, hjust=1))
>> >
>> > to plot the data.
>> >
>> > The COVID Tracking Project publishes considerably more data than does
>> > the state of Ohio. However, The project supplies daily statistics using
>> > the date format MMDD.I have done some searching, but I can't seem to
>> > find a solution (that I can understand).
>> >
>> > How can I change the date forma from MMDD tp -MM-DD?
>> >
>> > Thanks is advanced.
>> >
>> > --
>> > Stephen P. Molnar, Ph.D.
>> > www.molecular-modeling.net
>> > 614.312.7528 (c)
>> > Skype:  smolnar1
>> >
>> > __
>> > R-help@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.
>> >
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@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.
>>
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date Conversion Problem

2020-08-12 Thread Bert Gunter
Extra packages are not needed.

My question is: why change the character representation at all?  See the
format argument of ?as.Date.

> as.Date("20010102",format="%Y%m%d")
[1] "2001-01-02" ## the default format for the print method for Date objects


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Aug 12, 2020 at 8:07 AM Eric Berger  wrote:

> library(lubridate)
> a <- "20200403"
> lubridate::ymd(a)
> # 2020-04-03
>
> HTH,
> Eric
>
>
> On Wed, Aug 12, 2020 at 5:57 PM Stephen P. Molnar 
> wrote:
>
> > i have written an R script which allow me to plot the number of Covid-10
> > cases reported by he state of Ohio. In that se t of data the date format
> > is in the form -mm-dd.
> >
> > My script uses:
> >
> > datebreaks <- seq(as.Date("2020-01-01"), as.Date("2020-08-10"), by="1
> > week")
> > .
> > .
> > .
> >+ scale_x_date(breaks=datebreaks)
> >+ theme(axis.text.x = element_text(angle=30, hjust=1))
> >
> > to plot the data.
> >
> > The COVID Tracking Project publishes considerably more data than does
> > the state of Ohio. However, The project supplies daily statistics using
> > the date format MMDD.I have done some searching, but I can't seem to
> > find a solution (that I can understand).
> >
> > How can I change the date forma from MMDD tp -MM-DD?
> >
> > Thanks is advanced.
> >
> > --
> > Stephen P. Molnar, Ph.D.
> > www.molecular-modeling.net
> > 614.312.7528 (c)
> > Skype:  smolnar1
> >
> > __
> > R-help@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.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date Conversion Problem

2020-08-12 Thread Eric Berger
library(lubridate)
a <- "20200403"
lubridate::ymd(a)
# 2020-04-03

HTH,
Eric


On Wed, Aug 12, 2020 at 5:57 PM Stephen P. Molnar 
wrote:

> i have written an R script which allow me to plot the number of Covid-10
> cases reported by he state of Ohio. In that se t of data the date format
> is in the form -mm-dd.
>
> My script uses:
>
> datebreaks <- seq(as.Date("2020-01-01"), as.Date("2020-08-10"), by="1
> week")
> .
> .
> .
>+ scale_x_date(breaks=datebreaks)
>+ theme(axis.text.x = element_text(angle=30, hjust=1))
>
> to plot the data.
>
> The COVID Tracking Project publishes considerably more data than does
> the state of Ohio. However, The project supplies daily statistics using
> the date format MMDD.I have done some searching, but I can't seem to
> find a solution (that I can understand).
>
> How can I change the date forma from MMDD tp -MM-DD?
>
> Thanks is advanced.
>
> --
> Stephen P. Molnar, Ph.D.
> www.molecular-modeling.net
> 614.312.7528 (c)
> Skype:  smolnar1
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] Date Conversion Problem

2020-08-12 Thread Stephen P. Molnar
i have written an R script which allow me to plot the number of Covid-10 
cases reported by he state of Ohio. In that se t of data the date format 
is in the form -mm-dd.


My script uses:

datebreaks <- seq(as.Date("2020-01-01"), as.Date("2020-08-10"), by="1 week")
   .
   .
   .
  + scale_x_date(breaks=datebreaks)
  + theme(axis.text.x = element_text(angle=30, hjust=1))

to plot the data.

The COVID Tracking Project publishes considerably more data than does 
the state of Ohio. However, The project supplies daily statistics using 
the date format MMDD.I have done some searching, but I can't seem to 
find a solution (that I can understand).


How can I change the date forma from MMDD tp -MM-DD?

Thanks is advanced.

--
Stephen P. Molnar, Ph.D.
www.molecular-modeling.net
614.312.7528 (c)
Skype:  smolnar1

__
R-help@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.


Re: [R] Date from text

2020-05-15 Thread Enrico Schumann
On Fri, 15 May 2020, Poizot Emmanuel writes:

> Dear all,
>
> I've a data frame with a column "Date":
>
> [1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996
> [10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998 02-1998
> [19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001 10-2001
> [28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003 02-2003
> [37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005 11-2005
> [46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007 11-2007
> [55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009 03-2009
> [64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012 10-2012
> [73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018 12-2018
> [82] 12-2018 12-2018 12-2018
>
> I want to convert that into real dates:
> as.POSIXct(Date, format="%m-%Y") always return "NA" values.
> Where am I wrong ?
>
> regards

If you really want a date, I'd suggest 'as.Date'. The help
for ?as.Date says:

  "If the date string does not specify the date completely,
   the returned answer may be system-specific."

So perhaps try something like

as.Date(paste0("01-", "11-1993"), format = "%d-%m-%Y")
## [1] "1993-11-01"

Or look at 'yearmon' in package 'zoo':

library("zoo")
as.yearmon("11-1993", format = "%m-%Y")
## [1] "Nov 1993"

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-help@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.


Re: [R] Date from text

2020-05-15 Thread Marc Schwartz via R-help
Hi, 

For the usual R text to date conversions, you need a complete date. Since you 
are missing the day of the month in your source text, you would need to impute 
that part before making the conversion.

Also, since you don't appear to need to worry about time of day, just use 
as.Date(), instead of as.POSIXct().

In this case, use ?sub to add the day of the month to the source text, for 
which I will impute the 15th, and then make the conversion:

## Single value for now
x <- "11-1993"

> sub("-", "-15-", x)
[1] "11-15-1993"

> as.Date(sub("-", "-15-", x), format = "%m-%d-%Y")
[1] "1993-11-15"


Regards,

Marc Schwartz



> On May 15, 2020, at 5:38 AM, Poizot Emmanuel  
> wrote:
> 
> Dear all,
> 
> I've a data frame with a column "Date":
> 
> [1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996
> [10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998 02-1998
> [19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001 10-2001
> [28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003 02-2003
> [37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005 11-2005
> [46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007 11-2007
> [55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009 03-2009
> [64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012 10-2012
> [73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018 12-2018
> [82] 12-2018 12-2018 12-2018
> 
> I want to convert that into real dates:
> as.POSIXct(Date, format="%m-%Y") always return "NA" values.
> Where am I wrong ?
> 
> regards

__
R-help@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.


[R] Date from text

2020-05-15 Thread Poizot Emmanuel

Dear all,

I've a data frame with a column "Date":

[1] 11-1993 11-1993 11-1993 11-1993 11-1993 11-1993 11-1996 11-1996 11-1996
[10] 11-1996 11-1996 11-1996 02-1998 02-1998 02-1998 02-1998 02-1998 02-1998
[19] 11-1998 11-1998 11-1998 11-1998 11-1998 11-1998 10-2001 10-2001 10-2001
[28] 10-2001 10-2001 10-2001 02-2003 02-2003 02-2003 02-2003 02-2003 02-2003
[37] 11-2004 11-2004 11-2004 11-2004 11-2004 11-2004 11-2005 11-2005 11-2005
[46] 11-2005 11-2005 11-2005 11-2007 11-2007 11-2007 11-2007 11-2007 11-2007
[55] 10-2008 10-2008 10-2008 10-2008 10-2008 10-2008 03-2009 03-2009 03-2009
[64] 03-2009 03-2009 03-2009 10-2012 10-2012 10-2012 10-2012 10-2012 10-2012
[73] 12-2017 12-2017 12-2017 12-2017 12-2017 12-2017 12-2018 12-2018 12-2018
[82] 12-2018 12-2018 12-2018

I want to convert that into real dates:
as.POSIXct(Date, format="%m-%Y") always return "NA" values.
Where am I wrong ?

regards
__
R-help@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.


Re: [R] Date format

2020-05-12 Thread Rainer Hurling

Hi Medic,

Am 10.05.20 um 09:15 schrieb Medic:

I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
and applied to MY DATA, but got:
"Don't know how to automatically pick scale for object ..."
P.S. 1) R ver. 4.0 (Yes, Jeff);  2) Attached: mydata_dput (1 КБ)

SAMPLE CODE
library(ggplot2)
library(dplyr)
library(hrbrthemes)
data <- 
read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv
", header=T)

data$date <- as.Date(data$date)

# Plot
data %>%
   tail(10) %>%
   ggplot( aes(x=date, y=value)) +
 geom_line( color="grey") +
 geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
 theme_ipsum() +
 ggtitle("Evolution of bitcoin price")

==
MY DATA
mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")

str(mydata)
'data.frame': 7 obs. of  2 variables:
  $ date : chr  "01.01.2000" "02.01.2000" ...
  $ value: int  11 12 ...

mydata$date <- as.Date(mydata$date, "%d.%m.%Y")

str(mydata$date)
Date[1:7], format: "2000-01-01"

# Bert, thanks for the explanation!
# Rainer, thanks for the specific code!

# And then the problem:
mydata %>%
 tail(10) %>%
 ggplot( aes(x=mydata, y=value)) +
 geom_line( color="grey") +
 geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
 theme_ipsum() +
 ggtitle("Evolution")

"Don't know how to automatically pick scale for object of type
data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (7): x"


Perhaps only a little typo? Pls try

ggplot( aes(x=date, y=value))
  

__
R-help@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.


Re: [R] Date format

2020-05-10 Thread Medic
Many Thanks!!!
> cpolw...@chemo.org.uk:
> Your X axis is plotting mydata not date?
> Use aes(x=date

__
R-help@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.


Re: [R] Date format

2020-05-10 Thread Medic
I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
and applied to MY DATA, but got:
"Don't know how to automatically pick scale for object ..."
P.S. 1) R ver. 4.0 (Yes, Jeff);  2) Attached: mydata_dput (1 КБ)

SAMPLE CODE
library(ggplot2)
library(dplyr)
library(hrbrthemes)
data <- 
read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv
", header=T)

data$date <- as.Date(data$date)

# Plot
data %>%
  tail(10) %>%
  ggplot( aes(x=date, y=value)) +
geom_line( color="grey") +
geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
theme_ipsum() +
ggtitle("Evolution of bitcoin price")

==
MY DATA
mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")

str(mydata)
'data.frame': 7 obs. of  2 variables:
 $ date : chr  "01.01.2000" "02.01.2000" ...
 $ value: int  11 12 ...

mydata$date <- as.Date(mydata$date, "%d.%m.%Y")

str(mydata$date)
Date[1:7], format: "2000-01-01"

# Bert, thanks for the explanation!
# Rainer, thanks for the specific code!

# And then the problem:
mydata %>%
tail(10) %>%
ggplot( aes(x=mydata, y=value)) +
geom_line( color="grey") +
geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
theme_ipsum() +
ggtitle("Evolution")

"Don't know how to automatically pick scale for object of type
data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (7): x"
__
R-help@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.


Re: [R] Date format

2020-05-10 Thread Rainer Hurling
Am 10.05.20 um 04:17 schrieb Bert Gunter:
> $date is a factor, which is coded as numeric values internally, which
> as.date sees as numeric, and therefore:
> "as.Date will accept numeric data (the number of days since an epoch),
> but only if origin is supplied." (from ?as.Date)

as.Date is also able to read ISO date formatted strings by default (w/o
format).

as.Date("2013-04-28") works, as.Date("28.04.2013") not. The second
example needs as.Date("28.04.2013", format = "%d.%m.%Y").

> 
> You need to supply a format argument to as.Date to get it to handle
> the factor properly; e.g.
> "%d.%m.%Y"  should work. See ?strptime for formatting details.
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Sat, May 9, 2020 at 5:31 PM Medic  wrote:
>>
>> I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
>> and applied to MY DATA, but got:
>> "Error in as.Date.numeric(mydata$date) : 'origin' must be supplied".
>> P.S. I can not understand ?as.Date()
>>
>> SAMPLE CODE
>> library(ggplot2)
>> library(dplyr)
>> library(hrbrthemes)
>> data <- 
>> read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv;,
>> header=T)
>>
>> str(data)
>> 'data.frame': 1822 obs. of  2 variables:
>>  $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01" ...
>>  $ value: num  136 147 147 140 126 ...
>>
>> data$date <- as.Date(data$date)
>>
>> # Plot
>> data %>%
>>   tail(10) %>%
>>   ggplot( aes(x=date, y=value)) +
>> geom_line( color="grey") +
>> geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
>> theme_ipsum() +
>> ggtitle("Evolution of bitcoin price")
>>
>>
>> MY DATA
>> mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")
>>
>> str(mydata)
>> 'data.frame': 7 obs. of  2 variables:
>>  $ date : chr  "01.01.2000" "02.01.2000" "03.01.2000" "04.01.2000" ...
>>  $ value: int  11 12 13 14 15 16 17
>>
>> mydata$date <- as.Date(mydata$date)
>> Error in as.Date.numeric(mydata$date) : 'origin' must be supplied
>>
>> __
>> R-help@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.
> 
> __
> R-help@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.
>

__
R-help@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.


Re: [R] Date format

2020-05-09 Thread Bert Gunter
True. Whence the error message then?

Still, in my attempt to reproduce, the format statement worked.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, May 9, 2020 at 8:03 PM Jeff Newmiller  wrote:
>
> ... but str says it is character. This must be 4.0...
>
> On May 9, 2020 7:17:16 PM PDT, Bert Gunter  wrote:
> >$date is a factor, which is coded as numeric values internally, which
> >as.date sees as numeric, and therefore:
> >"as.Date will accept numeric data (the number of days since an epoch),
> >but only if origin is supplied." (from ?as.Date)
> >
> >You need to supply a format argument to as.Date to get it to handle
> >the factor properly; e.g.
> >"%d.%m.%Y"  should work. See ?strptime for formatting details.
> >
> >Bert Gunter
> >
> >"The trouble with having an open mind is that people keep coming along
> >and sticking things into it."
> >-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >On Sat, May 9, 2020 at 5:31 PM Medic  wrote:
> >>
> >> I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
> >> and applied to MY DATA, but got:
> >> "Error in as.Date.numeric(mydata$date) : 'origin' must be supplied".
> >> P.S. I can not understand ?as.Date()
> >>
> >> SAMPLE CODE
> >> library(ggplot2)
> >> library(dplyr)
> >> library(hrbrthemes)
> >> data <-
> >read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv;,
> >> header=T)
> >>
> >> str(data)
> >> 'data.frame': 1822 obs. of  2 variables:
> >>  $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01"
> >...
> >>  $ value: num  136 147 147 140 126 ...
> >>
> >> data$date <- as.Date(data$date)
> >>
> >> # Plot
> >> data %>%
> >>   tail(10) %>%
> >>   ggplot( aes(x=date, y=value)) +
> >> geom_line( color="grey") +
> >> geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
> >> theme_ipsum() +
> >> ggtitle("Evolution of bitcoin price")
> >>
> >>
> >> MY DATA
> >> mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")
> >>
> >> str(mydata)
> >> 'data.frame': 7 obs. of  2 variables:
> >>  $ date : chr  "01.01.2000" "02.01.2000" "03.01.2000" "04.01.2000"
> >...
> >>  $ value: int  11 12 13 14 15 16 17
> >>
> >> mydata$date <- as.Date(mydata$date)
> >> Error in as.Date.numeric(mydata$date) : 'origin' must be supplied
> >>
> >> __
> >> R-help@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.
> >
> >__
> >R-help@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.

__
R-help@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.


Re: [R] Date format

2020-05-09 Thread Jeff Newmiller
... but str says it is character. This must be 4.0...

On May 9, 2020 7:17:16 PM PDT, Bert Gunter  wrote:
>$date is a factor, which is coded as numeric values internally, which
>as.date sees as numeric, and therefore:
>"as.Date will accept numeric data (the number of days since an epoch),
>but only if origin is supplied." (from ?as.Date)
>
>You need to supply a format argument to as.Date to get it to handle
>the factor properly; e.g.
>"%d.%m.%Y"  should work. See ?strptime for formatting details.
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>On Sat, May 9, 2020 at 5:31 PM Medic  wrote:
>>
>> I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
>> and applied to MY DATA, but got:
>> "Error in as.Date.numeric(mydata$date) : 'origin' must be supplied".
>> P.S. I can not understand ?as.Date()
>>
>> SAMPLE CODE
>> library(ggplot2)
>> library(dplyr)
>> library(hrbrthemes)
>> data <-
>read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv;,
>> header=T)
>>
>> str(data)
>> 'data.frame': 1822 obs. of  2 variables:
>>  $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01"
>...
>>  $ value: num  136 147 147 140 126 ...
>>
>> data$date <- as.Date(data$date)
>>
>> # Plot
>> data %>%
>>   tail(10) %>%
>>   ggplot( aes(x=date, y=value)) +
>> geom_line( color="grey") +
>> geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
>> theme_ipsum() +
>> ggtitle("Evolution of bitcoin price")
>>
>>
>> MY DATA
>> mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")
>>
>> str(mydata)
>> 'data.frame': 7 obs. of  2 variables:
>>  $ date : chr  "01.01.2000" "02.01.2000" "03.01.2000" "04.01.2000"
>...
>>  $ value: int  11 12 13 14 15 16 17
>>
>> mydata$date <- as.Date(mydata$date)
>> Error in as.Date.numeric(mydata$date) : 'origin' must be supplied
>>
>> __
>> R-help@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.
>
>__
>R-help@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.

__
R-help@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.


Re: [R] Date format

2020-05-09 Thread Bert Gunter
$date is a factor, which is coded as numeric values internally, which
as.date sees as numeric, and therefore:
"as.Date will accept numeric data (the number of days since an epoch),
but only if origin is supplied." (from ?as.Date)

You need to supply a format argument to as.Date to get it to handle
the factor properly; e.g.
"%d.%m.%Y"  should work. See ?strptime for formatting details.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Sat, May 9, 2020 at 5:31 PM Medic  wrote:
>
> I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
> and applied to MY DATA, but got:
> "Error in as.Date.numeric(mydata$date) : 'origin' must be supplied".
> P.S. I can not understand ?as.Date()
>
> SAMPLE CODE
> library(ggplot2)
> library(dplyr)
> library(hrbrthemes)
> data <- 
> read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv;,
> header=T)
>
> str(data)
> 'data.frame': 1822 obs. of  2 variables:
>  $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01" ...
>  $ value: num  136 147 147 140 126 ...
>
> data$date <- as.Date(data$date)
>
> # Plot
> data %>%
>   tail(10) %>%
>   ggplot( aes(x=date, y=value)) +
> geom_line( color="grey") +
> geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
> theme_ipsum() +
> ggtitle("Evolution of bitcoin price")
>
>
> MY DATA
> mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")
>
> str(mydata)
> 'data.frame': 7 obs. of  2 variables:
>  $ date : chr  "01.01.2000" "02.01.2000" "03.01.2000" "04.01.2000" ...
>  $ value: int  11 12 13 14 15 16 17
>
> mydata$date <- as.Date(mydata$date)
> Error in as.Date.numeric(mydata$date) : 'origin' must be supplied
>
> __
> R-help@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.

__
R-help@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.


[R] Date format

2020-05-09 Thread Medic
I took a SAMPLE CODE (for Connected scatterplot) from the R gallery
and applied to MY DATA, but got:
"Error in as.Date.numeric(mydata$date) : 'origin' must be supplied".
P.S. I can not understand ?as.Date()

SAMPLE CODE
library(ggplot2)
library(dplyr)
library(hrbrthemes)
data <- 
read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv;,
header=T)

str(data)
'data.frame': 1822 obs. of  2 variables:
 $ date : chr  "2013-04-28" "2013-04-29" "2013-04-30" "2013-05-01" ...
 $ value: num  136 147 147 140 126 ...

data$date <- as.Date(data$date)

# Plot
data %>%
  tail(10) %>%
  ggplot( aes(x=date, y=value)) +
geom_line( color="grey") +
geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
theme_ipsum() +
ggtitle("Evolution of bitcoin price")


MY DATA
mydata <- read.table("E:/mydata.csv", header=TRUE, sep=";", dec=",")

str(mydata)
'data.frame': 7 obs. of  2 variables:
 $ date : chr  "01.01.2000" "02.01.2000" "03.01.2000" "04.01.2000" ...
 $ value: int  11 12 13 14 15 16 17

mydata$date <- as.Date(mydata$date)
Error in as.Date.numeric(mydata$date) : 'origin' must be supplied

__
R-help@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.


Re: [R] date

2019-12-19 Thread David Stevens
Val

Another all-base R solution:

as.Date(strptime(gs$date,format="%m/%d/%y"))

or if you want to add a time field later

as.POSIXct(strptime(gs$date,format="%m/%d/%y")))

since strptime produces a list version of the date: class is POSIXt and the 
subclass is POSIXlt, that can be convenient for extracting elements like month, 
day of the year, minute, etc. The actual date/time information is returned as 
the number of seconds since 1970-01-01 00:00:00 UTC but the representation is 
as in your gs.d1 below.

It can be a bit tricky when daylight savings time comes into play but that's 
for another post.

David


On 12/17/2019 3:51 PM, Val wrote:

Hi All,

I wanted to to convert character date  mm/dd/yy  to -mm-dd
The sample data and my attempt is shown below

gs <-read.table(text="ID date
A1   09/27/03
A2   05/27/16
A3   01/25/13
A4   09/27/19",header=TRUE,stringsAsFactors=F)

Desired output
  ID date  d1
 A1 09/27/03 2003-09-27
 A2 05/27/16 2016-05-27
 A3 01/25/13 2012-04-25
 A4 09/27/19 2019-09-27

I used this
gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")

but I got NA's.

How do I get my desired result?
Thank you.

__
R-help@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.


--
David K Stevens, PhD, PE
Environmental Engineering Division
Civil and Environmental Engineering
Utah State University
8200 Old Main Hill
Logan, UT 83200-8200
(435) 797-3229
david.stev...@usu.edu

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date

2019-12-19 Thread Jeff Newmiller
I agree that having convenience functions can be in the spirit of R, but I find 
that lubridate puts the cart before the horse so I avoid it. Specifically, the 
conceptual sequence

- convert character to timestamp in GMT
- "fix" erroneous timestamps to correct time zone

more inefficient and error-prone than

- convert character to timestamp specifying correct timezone

or

- specify default timezone
- convert character to timestamp using default timezone

but apparently others disagree so we have a whole sub-culture working with GMT 
by default.

On December 19, 2019 4:45:41 AM PST, Eric Berger  wrote:
>[ ... taking the bait regarding the "unnecessary discussion" ... ]
>
>The "Fortune nomination" that Bert sent includes the phrase
>
>"...then it is best to tell R ..."
>
>What metric is being used to do the ranking to get the "best"? If the
>metric is related to "providing the most unambiguous information to R"
>then I agree that providing the structure explicitly is best.
>However, often what is "best" is to minimize programmer time. With
>lubridate, I know that providing the clue 'ymd' is enough to have it
>perform the date conversion correctly. That is minimal effort on my
>part,
>which gives it a top ranking from my point of view.
>
>Also, to broaden this "unnecessary discussion" I would argue that the
>lubridate package may even be more "in the spirit of R" than what is
>being
>proposed with the explicit structural information. Clearly R is far
>from
>being a strongly typed language. If you really want to provide explicit
>structural information maybe you would be better off with a language
>such
>as C++. :-)
>
>
>
>
>
>On Thu, Dec 19, 2019 at 2:30 PM Bert Gunter 
>wrote:
>
>>
>>
>> "But the important point is:
>> If you know the structure of the data you want to
>> parse, then it is best to tell R (or any other language)
>> this structure explicitly. "
>>
>> Fortune nomination!
>>
>> -- Bert
>>
>>
>>
>>
>>
>>
>> Thu, Dec 19, 2019, 2:49 AM Enrico Schumann 
>wrote:
>>
>>>
>>> Quoting Eric Berger :
>>>
>>> > Martin  writes: "there's really no reason for going beyond base R"
>>> >
>>> > I disagree. Lubridate is a fantastic package. I use it all the
>time. It
>>> > makes working with dates really easy, as evidenced by John Kane's
>>> > suggestion. I strongly recommend learning to work with it.
>>> >
>>> > The bottom line: as is often the case, there are many different
>ways to
>>> > accomplish a task in R.
>>>
>>> I apologise beforehand if this sparks an unnecessary discussion ;-)
>>>
>>> But the important point is:
>>> If you know the structure of the data you want to
>>> parse, then it is best to tell R (or any other language)
>>> this structure explicitly.
>>>
>>>
>>> > On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler <
>>> maech...@stat.math.ethz.ch>
>>> > wrote:
>>> >
>>> >> > John Kane
>>> >> > on Tue, 17 Dec 2019 20:28:17 -0500 writes:
>>> >>
>>> >> > library(lubridate)
>>> >> > gs$dat1  <-  mdy(gs$date)
>>> >>
>>> >> there's really no reason for going beyond base R.
>>> >>
>>> >> Using the proper format as per Patrick and Peter's advice
>>> >> (below) is perfectly clear and actually
>>> >> more robust (for the next data set etc)
>>> >> than going via "good guessing" in extra packages.
>>> >>
>>> >> > On Tue, 17 Dec 2019 at 18:38, peter dalgaard
>
>>> >> wrote:
>>> >> >>
>>> >> >> ...and switch the order, and use %y for 2-digit years.
>>> >> >>
>>> >> >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative)
><
>>> >> mal...@malonequantitative.com> wrote:
>>> >> >> >
>>> >> >> > Try putting / instead of - in your format, to match the
>data.
>>> >> >> >
>>> >> >> > On Tue, Dec 17, 2019 at 5:52 PM Val 
>>> wrote:
>>> >> >> >>
>>> >> >> >> Hi All,
>>> >> >> >>
>>> >> >> >> I wanted to to convert character date  mm/dd/yy  to
>>> -mm-dd
>>> >> >> >> The sample data and my attempt is shown below
>>> >> >> >>
>>> >> >> >> gs <-read.table(text="ID date
>>> >> >> >> A1   09/27/03
>>> >> >> >> A2   05/27/16
>>> >> >> >> A3   01/25/13
>>> >> >> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>>> >> >> >>
>>> >> >> >> Desired output
>>> >> >> >>  ID date  d1
>>> >> >> >> A1 09/27/03 2003-09-27
>>> >> >> >> A2 05/27/16 2016-05-27
>>> >> >> >> A3 01/25/13 2012-04-25
>>> >> >> >> A4 09/27/19 2019-09-27
>>> >> >> >>
>>> >> >> >> I used this
>>> >> >> >> gs$d1 = as.Date(as.character(gs$date), format =
>"%Y-%m-%d")
>>> >> >> >>
>>> >> >> >> but I got NA's.
>>> >> >> >>
>>> >> >> >> How do I get my desired result?
>>> >> >> >> Thank you.
>>> >> >> >>
>>> >> >>
>>> >> >> --
>>> >> >> Peter Dalgaard, Professor,
>>> >> >> Center for Statistics, Copenhagen Business School
>>> >> >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>>> >> >> Phone: (+45)38153501
>>> >> >> Office: A 4.23
>>> >> >> Email: 

Re: [R] date

2019-12-19 Thread Eric Berger
[ ... taking the bait regarding the "unnecessary discussion" ... ]

The "Fortune nomination" that Bert sent includes the phrase

"...then it is best to tell R ..."

What metric is being used to do the ranking to get the "best"? If the
metric is related to "providing the most unambiguous information to R"
then I agree that providing the structure explicitly is best.
However, often what is "best" is to minimize programmer time. With
lubridate, I know that providing the clue 'ymd' is enough to have it
perform the date conversion correctly. That is minimal effort on my part,
which gives it a top ranking from my point of view.

Also, to broaden this "unnecessary discussion" I would argue that the
lubridate package may even be more "in the spirit of R" than what is being
proposed with the explicit structural information. Clearly R is far from
being a strongly typed language. If you really want to provide explicit
structural information maybe you would be better off with a language such
as C++. :-)





On Thu, Dec 19, 2019 at 2:30 PM Bert Gunter  wrote:

>
>
> "But the important point is:
> If you know the structure of the data you want to
> parse, then it is best to tell R (or any other language)
> this structure explicitly. "
>
> Fortune nomination!
>
> -- Bert
>
>
>
>
>
>
> Thu, Dec 19, 2019, 2:49 AM Enrico Schumann  wrote:
>
>>
>> Quoting Eric Berger :
>>
>> > Martin  writes: "there's really no reason for going beyond base R"
>> >
>> > I disagree. Lubridate is a fantastic package. I use it all the time. It
>> > makes working with dates really easy, as evidenced by John Kane's
>> > suggestion. I strongly recommend learning to work with it.
>> >
>> > The bottom line: as is often the case, there are many different ways to
>> > accomplish a task in R.
>>
>> I apologise beforehand if this sparks an unnecessary discussion ;-)
>>
>> But the important point is:
>> If you know the structure of the data you want to
>> parse, then it is best to tell R (or any other language)
>> this structure explicitly.
>>
>>
>> > On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler <
>> maech...@stat.math.ethz.ch>
>> > wrote:
>> >
>> >> > John Kane
>> >> > on Tue, 17 Dec 2019 20:28:17 -0500 writes:
>> >>
>> >> > library(lubridate)
>> >> > gs$dat1  <-  mdy(gs$date)
>> >>
>> >> there's really no reason for going beyond base R.
>> >>
>> >> Using the proper format as per Patrick and Peter's advice
>> >> (below) is perfectly clear and actually
>> >> more robust (for the next data set etc)
>> >> than going via "good guessing" in extra packages.
>> >>
>> >> > On Tue, 17 Dec 2019 at 18:38, peter dalgaard 
>> >> wrote:
>> >> >>
>> >> >> ...and switch the order, and use %y for 2-digit years.
>> >> >>
>> >> >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
>> >> mal...@malonequantitative.com> wrote:
>> >> >> >
>> >> >> > Try putting / instead of - in your format, to match the data.
>> >> >> >
>> >> >> > On Tue, Dec 17, 2019 at 5:52 PM Val 
>> wrote:
>> >> >> >>
>> >> >> >> Hi All,
>> >> >> >>
>> >> >> >> I wanted to to convert character date  mm/dd/yy  to
>> -mm-dd
>> >> >> >> The sample data and my attempt is shown below
>> >> >> >>
>> >> >> >> gs <-read.table(text="ID date
>> >> >> >> A1   09/27/03
>> >> >> >> A2   05/27/16
>> >> >> >> A3   01/25/13
>> >> >> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>> >> >> >>
>> >> >> >> Desired output
>> >> >> >>  ID date  d1
>> >> >> >> A1 09/27/03 2003-09-27
>> >> >> >> A2 05/27/16 2016-05-27
>> >> >> >> A3 01/25/13 2012-04-25
>> >> >> >> A4 09/27/19 2019-09-27
>> >> >> >>
>> >> >> >> I used this
>> >> >> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
>> >> >> >>
>> >> >> >> but I got NA's.
>> >> >> >>
>> >> >> >> How do I get my desired result?
>> >> >> >> Thank you.
>> >> >> >>
>> >> >>
>> >> >> --
>> >> >> Peter Dalgaard, Professor,
>> >> >> Center for Statistics, Copenhagen Business School
>> >> >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> >> >> Phone: (+45)38153501
>> >> >> Office: A 4.23
>> >> >> Email: pd@cbs.dk  Priv: pda...@gmail.com
>> >> >>
>> >>
>> >> > --
>> >> > John Kane
>> >> > Kingston ON Canada
>>
>>
>>
>> --
>> Enrico Schumann
>> Lucerne, Switzerland
>> http://enricoschumann.net
>>
>> __
>> R-help@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.
>>
>>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the 

Re: [R] date

2019-12-19 Thread Bert Gunter
"But the important point is:
If you know the structure of the data you want to
parse, then it is best to tell R (or any other language)
this structure explicitly. "

Fortune nomination!

-- Bert






Thu, Dec 19, 2019, 2:49 AM Enrico Schumann  wrote:

>
> Quoting Eric Berger :
>
> > Martin  writes: "there's really no reason for going beyond base R"
> >
> > I disagree. Lubridate is a fantastic package. I use it all the time. It
> > makes working with dates really easy, as evidenced by John Kane's
> > suggestion. I strongly recommend learning to work with it.
> >
> > The bottom line: as is often the case, there are many different ways to
> > accomplish a task in R.
>
> I apologise beforehand if this sparks an unnecessary discussion ;-)
>
> But the important point is:
> If you know the structure of the data you want to
> parse, then it is best to tell R (or any other language)
> this structure explicitly.
>
>
> > On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler <
> maech...@stat.math.ethz.ch>
> > wrote:
> >
> >> > John Kane
> >> > on Tue, 17 Dec 2019 20:28:17 -0500 writes:
> >>
> >> > library(lubridate)
> >> > gs$dat1  <-  mdy(gs$date)
> >>
> >> there's really no reason for going beyond base R.
> >>
> >> Using the proper format as per Patrick and Peter's advice
> >> (below) is perfectly clear and actually
> >> more robust (for the next data set etc)
> >> than going via "good guessing" in extra packages.
> >>
> >> > On Tue, 17 Dec 2019 at 18:38, peter dalgaard 
> >> wrote:
> >> >>
> >> >> ...and switch the order, and use %y for 2-digit years.
> >> >>
> >> >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
> >> mal...@malonequantitative.com> wrote:
> >> >> >
> >> >> > Try putting / instead of - in your format, to match the data.
> >> >> >
> >> >> > On Tue, Dec 17, 2019 at 5:52 PM Val 
> wrote:
> >> >> >>
> >> >> >> Hi All,
> >> >> >>
> >> >> >> I wanted to to convert character date  mm/dd/yy  to -mm-dd
> >> >> >> The sample data and my attempt is shown below
> >> >> >>
> >> >> >> gs <-read.table(text="ID date
> >> >> >> A1   09/27/03
> >> >> >> A2   05/27/16
> >> >> >> A3   01/25/13
> >> >> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
> >> >> >>
> >> >> >> Desired output
> >> >> >>  ID date  d1
> >> >> >> A1 09/27/03 2003-09-27
> >> >> >> A2 05/27/16 2016-05-27
> >> >> >> A3 01/25/13 2012-04-25
> >> >> >> A4 09/27/19 2019-09-27
> >> >> >>
> >> >> >> I used this
> >> >> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
> >> >> >>
> >> >> >> but I got NA's.
> >> >> >>
> >> >> >> How do I get my desired result?
> >> >> >> Thank you.
> >> >> >>
> >> >>
> >> >> --
> >> >> Peter Dalgaard, Professor,
> >> >> Center for Statistics, Copenhagen Business School
> >> >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> >> >> Phone: (+45)38153501
> >> >> Office: A 4.23
> >> >> Email: pd@cbs.dk  Priv: pda...@gmail.com
> >> >>
> >>
> >> > --
> >> > John Kane
> >> > Kingston ON Canada
>
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
> __
> R-help@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.
>
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date

2019-12-19 Thread Enrico Schumann



Quoting Eric Berger :


Martin  writes: "there's really no reason for going beyond base R"

I disagree. Lubridate is a fantastic package. I use it all the time. It
makes working with dates really easy, as evidenced by John Kane's
suggestion. I strongly recommend learning to work with it.

The bottom line: as is often the case, there are many different ways to
accomplish a task in R.


I apologise beforehand if this sparks an unnecessary discussion ;-)

But the important point is:
If you know the structure of the data you want to
parse, then it is best to tell R (or any other language)
this structure explicitly.



On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler 
wrote:


> John Kane
> on Tue, 17 Dec 2019 20:28:17 -0500 writes:

> library(lubridate)
> gs$dat1  <-  mdy(gs$date)

there's really no reason for going beyond base R.

Using the proper format as per Patrick and Peter's advice
(below) is perfectly clear and actually
more robust (for the next data set etc)
than going via "good guessing" in extra packages.

> On Tue, 17 Dec 2019 at 18:38, peter dalgaard 
wrote:
>>
>> ...and switch the order, and use %y for 2-digit years.
>>
>> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
mal...@malonequantitative.com> wrote:
>> >
>> > Try putting / instead of - in your format, to match the data.
>> >
>> > On Tue, Dec 17, 2019 at 5:52 PM Val  wrote:
>> >>
>> >> Hi All,
>> >>
>> >> I wanted to to convert character date  mm/dd/yy  to -mm-dd
>> >> The sample data and my attempt is shown below
>> >>
>> >> gs <-read.table(text="ID date
>> >> A1   09/27/03
>> >> A2   05/27/16
>> >> A3   01/25/13
>> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>> >>
>> >> Desired output
>> >>  ID date  d1
>> >> A1 09/27/03 2003-09-27
>> >> A2 05/27/16 2016-05-27
>> >> A3 01/25/13 2012-04-25
>> >> A4 09/27/19 2019-09-27
>> >>
>> >> I used this
>> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
>> >>
>> >> but I got NA's.
>> >>
>> >> How do I get my desired result?
>> >> Thank you.
>> >>
>>
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd@cbs.dk  Priv: pda...@gmail.com
>>

> --
> John Kane
> Kingston ON Canada




--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
R-help@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.


Re: [R] date

2019-12-19 Thread Eric Berger
Martin  writes: "there's really no reason for going beyond base R"

I disagree. Lubridate is a fantastic package. I use it all the time. It
makes working with dates really easy, as evidenced by John Kane's
suggestion. I strongly recommend learning to work with it.

The bottom line: as is often the case, there are many different ways to
accomplish a task in R.


On Thu, Dec 19, 2019 at 10:31 AM Martin Maechler 
wrote:

> > John Kane
> > on Tue, 17 Dec 2019 20:28:17 -0500 writes:
>
> > library(lubridate)
> > gs$dat1  <-  mdy(gs$date)
>
> there's really no reason for going beyond base R.
>
> Using the proper format as per Patrick and Peter's advice
> (below) is perfectly clear and actually
> more robust (for the next data set etc)
> than going via "good guessing" in extra packages.
>
> > On Tue, 17 Dec 2019 at 18:38, peter dalgaard 
> wrote:
> >>
> >> ...and switch the order, and use %y for 2-digit years.
> >>
> >> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) <
> mal...@malonequantitative.com> wrote:
> >> >
> >> > Try putting / instead of - in your format, to match the data.
> >> >
> >> > On Tue, Dec 17, 2019 at 5:52 PM Val  wrote:
> >> >>
> >> >> Hi All,
> >> >>
> >> >> I wanted to to convert character date  mm/dd/yy  to -mm-dd
> >> >> The sample data and my attempt is shown below
> >> >>
> >> >> gs <-read.table(text="ID date
> >> >> A1   09/27/03
> >> >> A2   05/27/16
> >> >> A3   01/25/13
> >> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
> >> >>
> >> >> Desired output
> >> >>  ID date  d1
> >> >> A1 09/27/03 2003-09-27
> >> >> A2 05/27/16 2016-05-27
> >> >> A3 01/25/13 2012-04-25
> >> >> A4 09/27/19 2019-09-27
> >> >>
> >> >> I used this
> >> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
> >> >>
> >> >> but I got NA's.
> >> >>
> >> >> How do I get my desired result?
> >> >> Thank you.
> >> >>
> >> >> __
> >> >> R-help@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.
> >> >
> >> > __
> >> > R-help@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.
> >>
> >> --
> >> Peter Dalgaard, Professor,
> >> Center for Statistics, Copenhagen Business School
> >> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> >> Phone: (+45)38153501
> >> Office: A 4.23
> >> Email: pd@cbs.dk  Priv: pda...@gmail.com
> >>
> >> __
> >> R-help@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.
>
>
>
> > --
> > John Kane
> > Kingston ON Canada
>
> > __
> > R-help@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.
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date

2019-12-19 Thread Martin Maechler
> John Kane 
> on Tue, 17 Dec 2019 20:28:17 -0500 writes:

> library(lubridate)
> gs$dat1  <-  mdy(gs$date)

there's really no reason for going beyond base R.

Using the proper format as per Patrick and Peter's advice
(below) is perfectly clear and actually
more robust (for the next data set etc)
than going via "good guessing" in extra packages.

> On Tue, 17 Dec 2019 at 18:38, peter dalgaard  wrote:
>> 
>> ...and switch the order, and use %y for 2-digit years.
>> 
>> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) 
 wrote:
>> >
>> > Try putting / instead of - in your format, to match the data.
>> >
>> > On Tue, Dec 17, 2019 at 5:52 PM Val  wrote:
>> >>
>> >> Hi All,
>> >>
>> >> I wanted to to convert character date  mm/dd/yy  to -mm-dd
>> >> The sample data and my attempt is shown below
>> >>
>> >> gs <-read.table(text="ID date
>> >> A1   09/27/03
>> >> A2   05/27/16
>> >> A3   01/25/13
>> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>> >>
>> >> Desired output
>> >>  ID date  d1
>> >> A1 09/27/03 2003-09-27
>> >> A2 05/27/16 2016-05-27
>> >> A3 01/25/13 2012-04-25
>> >> A4 09/27/19 2019-09-27
>> >>
>> >> I used this
>> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
>> >>
>> >> but I got NA's.
>> >>
>> >> How do I get my desired result?
>> >> Thank you.
>> >>
>> >> __
>> >> R-help@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.
>> >
>> > __
>> > R-help@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.
>> 
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd@cbs.dk  Priv: pda...@gmail.com
>> 
>> __
>> R-help@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.



> -- 
> John Kane
> Kingston ON Canada

> __
> R-help@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.

__
R-help@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.


Re: [R] date

2019-12-17 Thread John Kane
library(lubridate)
gs$dat1  <-  mdy(gs$date)

On Tue, 17 Dec 2019 at 18:38, peter dalgaard  wrote:
>
> ...and switch the order, and use %y for 2-digit years.
>
> > On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) 
> >  wrote:
> >
> > Try putting / instead of - in your format, to match the data.
> >
> > On Tue, Dec 17, 2019 at 5:52 PM Val  wrote:
> >>
> >> Hi All,
> >>
> >> I wanted to to convert character date  mm/dd/yy  to -mm-dd
> >> The sample data and my attempt is shown below
> >>
> >> gs <-read.table(text="ID date
> >> A1   09/27/03
> >> A2   05/27/16
> >> A3   01/25/13
> >> A4   09/27/19",header=TRUE,stringsAsFactors=F)
> >>
> >> Desired output
> >>  ID date  d1
> >> A1 09/27/03 2003-09-27
> >> A2 05/27/16 2016-05-27
> >> A3 01/25/13 2012-04-25
> >> A4 09/27/19 2019-09-27
> >>
> >> I used this
> >> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
> >>
> >> but I got NA's.
> >>
> >> How do I get my desired result?
> >> Thank you.
> >>
> >> __
> >> R-help@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.
> >
> > __
> > R-help@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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd@cbs.dk  Priv: pda...@gmail.com
>
> __
> R-help@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.



-- 
John Kane
Kingston ON Canada

__
R-help@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.


Re: [R] date

2019-12-17 Thread peter dalgaard
...and switch the order, and use %y for 2-digit years.

> On 17 Dec 2019, at 23:57 , Patrick (Malone Quantitative) 
>  wrote:
> 
> Try putting / instead of - in your format, to match the data.
> 
> On Tue, Dec 17, 2019 at 5:52 PM Val  wrote:
>> 
>> Hi All,
>> 
>> I wanted to to convert character date  mm/dd/yy  to -mm-dd
>> The sample data and my attempt is shown below
>> 
>> gs <-read.table(text="ID date
>> A1   09/27/03
>> A2   05/27/16
>> A3   01/25/13
>> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>> 
>> Desired output
>>  ID date  d1
>> A1 09/27/03 2003-09-27
>> A2 05/27/16 2016-05-27
>> A3 01/25/13 2012-04-25
>> A4 09/27/19 2019-09-27
>> 
>> I used this
>> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
>> 
>> but I got NA's.
>> 
>> How do I get my desired result?
>> Thank you.
>> 
>> __
>> R-help@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.
> 
> __
> R-help@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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


Re: [R] date

2019-12-17 Thread Patrick (Malone Quantitative)
Try putting / instead of - in your format, to match the data.

On Tue, Dec 17, 2019 at 5:52 PM Val  wrote:
>
> Hi All,
>
> I wanted to to convert character date  mm/dd/yy  to -mm-dd
> The sample data and my attempt is shown below
>
> gs <-read.table(text="ID date
> A1   09/27/03
> A2   05/27/16
> A3   01/25/13
> A4   09/27/19",header=TRUE,stringsAsFactors=F)
>
> Desired output
>   ID date  d1
>  A1 09/27/03 2003-09-27
>  A2 05/27/16 2016-05-27
>  A3 01/25/13 2012-04-25
>  A4 09/27/19 2019-09-27
>
> I used this
> gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")
>
> but I got NA's.
>
> How do I get my desired result?
> Thank you.
>
> __
> R-help@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.

__
R-help@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.


[R] date

2019-12-17 Thread Val
Hi All,

I wanted to to convert character date  mm/dd/yy  to -mm-dd
The sample data and my attempt is shown below

gs <-read.table(text="ID date
A1   09/27/03
A2   05/27/16
A3   01/25/13
A4   09/27/19",header=TRUE,stringsAsFactors=F)

Desired output
  ID date  d1
 A1 09/27/03 2003-09-27
 A2 05/27/16 2016-05-27
 A3 01/25/13 2012-04-25
 A4 09/27/19 2019-09-27

I used this
gs$d1 = as.Date(as.character(gs$date), format = "%Y-%m-%d")

but I got NA's.

How do I get my desired result?
Thank you.

__
R-help@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.


[R] Date Time Conversion

2019-05-08 Thread reichmanj
r-Help Community

 

Never mine figured it out just use the "as.POSIXct" function

 

Jeff

 

I need to convert a date-time field (column)  but I'm losing the time  when
I convert using  ..

 

tsData <- myData[,10, drop=FALSE]

tsData$date_time <- as.Date(tsData$date_time, format="%m/%d/%y %H:%M")

head(tsData)

 


 

 

date_time





1

2013-06-20



2

2013-06-20



3

2013-06-20



4

2013-06-20



5

2013-05-30



6

2013-06-20



 

R is doing what I'm asing it to do so I'm obviously using the wrong command.
How do I convert, retaining the time 

 

Sincerely

 

Jeffery (Jeff) Reichman


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date Time Conversion

2019-05-08 Thread Rui Barradas

Hello,

as.Date() outputs an object of class "Date", you want an object of class 
c("POSIXt", "POSIXct"). Use as.POSIXct().


Hope this helps,

Rui Barradas

Às 16:04 de 08/05/19, reichm...@sbcglobal.net escreveu:

r-Help Community

  


I need to convert a date-time field (column)  but I'm losing the time  when
I convert using  ..

  


tsData <- myData[,10, drop=FALSE]

tsData$date_time <- as.Date(tsData$date_time, format="%m/%d/%y %H:%M")

head(tsData)

  



  

  


date_time





1

2013-06-20



2

2013-06-20



3

2013-06-20



4

2013-06-20



5

2013-05-30



6

2013-06-20



  


R is doing what I'm asing it to do so I'm obviously using the wrong command.
How do I convert, retaining the time

  


Sincerely

  


Jeffery (Jeff) Reichman


[[alternative HTML version deleted]]

__
R-help@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.



__
R-help@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.


[R] Date Time Conversion

2019-05-08 Thread reichmanj
r-Help Community

 

I need to convert a date-time field (column)  but I'm losing the time  when
I convert using  ..

 

tsData <- myData[,10, drop=FALSE]

tsData$date_time <- as.Date(tsData$date_time, format="%m/%d/%y %H:%M")

head(tsData)

 


 

 

date_time





1

2013-06-20



2

2013-06-20



3

2013-06-20



4

2013-06-20



5

2013-05-30



6

2013-06-20



 

R is doing what I'm asing it to do so I'm obviously using the wrong command.
How do I convert, retaining the time 

 

Sincerely

 

Jeffery (Jeff) Reichman


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-11-03 Thread snowball0916
Hi, Bert
I will check those two, by the way, how can you find the two function? have you 
used them before?
Thanks very much.
 


 
From: Bert Gunter
Date: 2018-11-04 10:07
To: snowball0916
CC: MacQueen, Don; R-help
Subject: Re: [R] date and time data on x axis
See ?identify and ?locator

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and 
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Nov 3, 2018 at 6:47 PM snowball0916  wrote:
Hi, Don
After I've tried 1 month data. It show me like attachment.

The problem is it's hard to identify the the high point from the graph.
Is there possible, when my cursor move on some point , it will show me both x 
axis and y axis data?
OR 
Is there other way to get the same goal?

Thanks very much.





From: MacQueen, Don
Date: 2018-10-30 00:01
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Here's an example of 24 hours of data at one second intervals.

npts <- 24*60*60

df <- data.frame(
 tm = seq( Sys.time(), by='1 sec', length=npts),
 yd = round(runif(npts),2)
 )

head(df)

with(df, plot(tm,yd))

The x axis appears to me to be displayed in a neat and clean way. I don't 
understand what the problem is.
(The data itself is illegible, but that's a different problem.)

The default axis may not be what you want, but it is neat and clean. To choose 
the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui 
mentioned.  help(axis.POSIXct) provides examples of actual use.

I prefer to do as much as possible with base R, so look at this example:

> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"

Therefore 
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)


par() is the function that sets graphical parameters. There are many graphical 
parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par 
)

This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.

Type the following commands
   par('mar')
   par()$mar## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')## to see that the margins have been changed

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509

On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" 
 wrote:

Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=
Sample Data
=
The sample data look like this:

20181028_10:00:00 600 
20181028_10:00:01 500 
20181028_10:00:02 450 
20181028_10:00:03 660
..

=
My Code
=

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=
Questions:
=

1. Why my x axis does not show me the correct date time like ""2018-10-28 
10:00:00 UTC" ?
2. If my data is very huge(like data in every second and the data has the 
whole day , even the whole month), how can I display the x axis in a neat and 
clean way?

Thanks very much.




[[alternative HTML version deleted]]

__
R-help@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.


__
R-help@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.

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-11-03 Thread Bert Gunter
See ?identify and ?locator

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Nov 3, 2018 at 6:47 PM snowball0916  wrote:

> Hi, Don
> After I've tried 1 month data. It show me like attachment.
>
> The problem is it's hard to identify the the high point from the graph.
> Is there possible, when my cursor move on some point , it will show me
> both x axis and y axis data?
> OR
> Is there other way to get the same goal?
>
> Thanks very much.
>
>
>
>
>
> From: MacQueen, Don
> Date: 2018-10-30 00:01
> To: snowball0916; r-help
> Subject: Re: [R] date and time data on x axis
> Here's an example of 24 hours of data at one second intervals.
>
> npts <- 24*60*60
>
> df <- data.frame(
>  tm = seq( Sys.time(), by='1 sec', length=npts),
>  yd = round(runif(npts),2)
>  )
>
> head(df)
>
> with(df, plot(tm,yd))
>
> The x axis appears to me to be displayed in a neat and clean way. I don't
> understand what the problem is.
> (The data itself is illegible, but that's a different problem.)
>
> The default axis may not be what you want, but it is neat and clean. To
> choose the axis tick marks and labels yourself, use axis() or axis.POSIXct,
> as Rui mentioned.  help(axis.POSIXct) provides examples of actual use.
>
> I prefer to do as much as possible with base R, so look at this example:
>
> > as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
> [1] "2018-10-28 10:00:00 PDT"
>
> Therefore
>   xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
> is perfectly adequate (the lubridate package is not essential here)
>
>
> par() is the function that sets graphical parameters. There are many
> graphical parameters.
> "mar" is the parameter that specifies the sizes of the plot margins  ( see
> ?par )
>
> This expression
>op <- par(mar = c(4, 0, 0, 0) + par("mar"))
> is a way to modify the values of the "mar" parameter.
>
> Type the following commands
>par('mar')
>par()$mar## an alternative
>c(4,0,0,0) + par('mar')
>par(mar = c(4, 0, 0, 0) + par("mar"))
>par('mar')## to see that the margins have been changed
>
> --
> Don MacQueen
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
> Lab cell 925-724-7509
>
> On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" <
> r-help-boun...@r-project.org on behalf of snowball0...@163.com> wrote:
>
> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =
> Sample Data
> =
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ..
>
> =
> My Code
> =
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =
> Questions:
> =
>
> 1. Why my x axis does not show me the correct date time like
> ""2018-10-28 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has
> the whole day , even the whole month), how can I display the x axis in a
> neat and clean way?
>
> Thanks very much.
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-11-03 Thread snowball0916
Hi, Don
After I've tried 1 month data. It show me like attachment.

The problem is it's hard to identify the the high point from the graph.
Is there possible, when my cursor move on some point , it will show me both x 
axis and y axis data?
OR 
Is there other way to get the same goal?

Thanks very much.
 
 


 
From: MacQueen, Don
Date: 2018-10-30 00:01
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Here's an example of 24 hours of data at one second intervals.
 
npts <- 24*60*60
 
df <- data.frame(
 tm = seq( Sys.time(), by='1 sec', length=npts),
 yd = round(runif(npts),2)
 )
 
head(df)
 
with(df, plot(tm,yd))
 
The x axis appears to me to be displayed in a neat and clean way. I don't 
understand what the problem is.
(The data itself is illegible, but that's a different problem.)
 
The default axis may not be what you want, but it is neat and clean. To choose 
the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui 
mentioned.  help(axis.POSIXct) provides examples of actual use.
 
I prefer to do as much as possible with base R, so look at this example:
 
> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"
 
Therefore 
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)
 
 
par() is the function that sets graphical parameters. There are many graphical 
parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par 
)
 
This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.
 
Type the following commands
   par('mar')
   par()$mar## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')## to see that the margins have been changed
 
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" 
 wrote:
 
Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=
Sample Data
=
The sample data look like this:

20181028_10:00:00 600 
20181028_10:00:01 500 
20181028_10:00:02 450 
20181028_10:00:03 660
..

=
My Code
=

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=
Questions:
=

1. Why my x axis does not show me the correct date time like ""2018-10-28 
10:00:00 UTC" ?
2. If my data is very huge(like data in every second and the data has the 
whole day , even the whole month), how can I display the x axis in a neat and 
clean way?

Thanks very much.
 



[[alternative HTML version deleted]]

__
R-help@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.

 
__
R-help@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.


Re: [R] date and time data on x axis

2018-10-30 Thread snowball0916
Hi, Don
I got it, I will try and study .
Thanks very much. 
 


 
From: MacQueen, Don
Date: 2018-10-30 00:01
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Here's an example of 24 hours of data at one second intervals.
 
npts <- 24*60*60
 
df <- data.frame(
 tm = seq( Sys.time(), by='1 sec', length=npts),
 yd = round(runif(npts),2)
 )
 
head(df)
 
with(df, plot(tm,yd))
 
The x axis appears to me to be displayed in a neat and clean way. I don't 
understand what the problem is.
(The data itself is illegible, but that's a different problem.)
 
The default axis may not be what you want, but it is neat and clean. To choose 
the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui 
mentioned.  help(axis.POSIXct) provides examples of actual use.
 
I prefer to do as much as possible with base R, so look at this example:
 
> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"
 
Therefore 
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)
 
 
par() is the function that sets graphical parameters. There are many graphical 
parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par 
)
 
This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.
 
Type the following commands
   par('mar')
   par()$mar## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')## to see that the margins have been changed
 
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" 
 wrote:
 
Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=
Sample Data
=
The sample data look like this:

20181028_10:00:00 600 
20181028_10:00:01 500 
20181028_10:00:02 450 
20181028_10:00:03 660
..

=
My Code
=

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=
Questions:
=

1. Why my x axis does not show me the correct date time like ""2018-10-28 
10:00:00 UTC" ?
2. If my data is very huge(like data in every second and the data has the 
whole day , even the whole month), how can I display the x axis in a neat and 
clean way?

Thanks very much.
 



[[alternative HTML version deleted]]

__
R-help@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.

 

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-10-30 Thread snowball0916
Hi, Rui
Thank you . I will try later.
Thanks again.

 
 


 
From: Rui Barradas
Date: 2018-10-30 00:38
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Hello,
 
Inline.
 
Às 14:03 de 29/10/2018, snowball0916 escreveu:
> Hi, Rui
> Thanks for your code, even though I'm not fully understand it.
> And I am new to R, could you kindly help me with
> -1). what's the nested usage stand for?
> op <- par(mar = c(4, 0, 0, 0) + par("mar"))
 
See the help for ?par. You have
 
mar
A numerical vector of the form c(bottom, left, top, right) which gives 
the number of lines of margin to be specified on the four sides of the 
plot. The default is c(5, 4, 4, 2) + 0.1.
 
 
So par("mar") returns c(5.1, 4.1, 4.1, 2.1)
In order to see the x axis labels, that are longer than normal, I have 
added 4 to the bottom only, c(4, 0, 0, 0) + default_value.
And par() returns the previous values of the graphices parameters, so 
save them in op (old param) and restore the defaults when done plotting.
 
> 
> -2). how can I get the axis.POSIXct usage or help?
> I use help(axis)and help(POSIXct)and not found the actual use.
 
It's ?axis.POSIXct or help('axis.POSIXct'). You want the S3 method for 
objects of class "POSIXct".
 
Hope this helps,
 
Rui Barradas
 
> 
> Thanks very much.
> 
> 
> 
> 
> 
> 
> *From:* Rui Barradas <mailto:ruipbarra...@sapo.pt>
> *Date:* 2018-10-29 02:53
> *To:* snowball0916 <mailto:snowball0...@163.com>; r-help
> <mailto:r-help@r-project.org>
> *Subject:* Re: [R] date and time data on x axis
> Hello,
> Maybe you could get some inspiration in the following code.
> op <- par(mar = c(4, 0, 0, 0) + par("mar"))
> plot(xdata, ydata, type = "o", xaxt = "n")
> axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)
> par(op)
> The important part is the call axis.POSIXct, argument las = 2 and the
> call to par is to make sure that the labels are visible.
> Hope this helps,
> Rui Barradas
> Às 15:16 de 28/10/2018, snowball0916 escreveu:
>  > Hi, guys
>  > How do you guys deal with the date and time data on x axis?
>  > I have some trouble with it. Could you help with this?
>  >
>  > =
>  > Sample Data
>  > =
>  > The sample data look like this:
>  >
>  > 20181028_10:00:00 600
>  > 20181028_10:00:01 500
>  > 20181028_10:00:02 450
>  > 20181028_10:00:03 660
>  > ..
>  >
>  > =
>  > My Code
>  > =
>  >
>  > library(lubridate)
>  > mydata <- read.table("e:/R_study/graph_test2.txt")
>  > xdata <- ymd_hms(mydata$V1)
>  > ydata <- mydata$V2
>  > plot(xdata, ydata, type="o")
>  >
>  >
>  > =
>  > Questions:
>  > =
>  >
>  > 1. Why my x axis does not show me the correct date time like
> ""2018-10-28 10:00:00 UTC" ?
>  > 2. If my data is very huge(like data in every second and the data
> has the whole day , even the whole month), how can I display the x
> axis in a neat and clean way?
>  >
>  > Thanks very much.
>  >
>  >
>  >
>  >
>  > [[alternative HTML version deleted]]
>  >
>  > __
>  > R-help@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.
>  >
> 

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-10-29 Thread Rui Barradas

Hello,

Inline.

Às 14:03 de 29/10/2018, snowball0916 escreveu:

Hi, Rui
Thanks for your code, even though I'm not fully understand it.
And I am new to R, could you kindly help me with
-1). what's the nested usage stand for?
op <- par(mar = c(4, 0, 0, 0) + par("mar"))


See the help for ?par. You have

mar
A numerical vector of the form c(bottom, left, top, right) which gives 
the number of lines of margin to be specified on the four sides of the 
plot. The default is c(5, 4, 4, 2) + 0.1.



So par("mar") returns c(5.1, 4.1, 4.1, 2.1)
In order to see the x axis labels, that are longer than normal, I have 
added 4 to the bottom only, c(4, 0, 0, 0) + default_value.
And par() returns the previous values of the graphices parameters, so 
save them in op (old param) and restore the defaults when done plotting.




-2). how can I get the axis.POSIXct usage or help?
I use help(axis)and help(POSIXct)and not found the actual use.


It's ?axis.POSIXct or help('axis.POSIXct'). You want the S3 method for 
objects of class "POSIXct".


Hope this helps,

Rui Barradas



Thanks very much.






*From:* Rui Barradas <mailto:ruipbarra...@sapo.pt>
*Date:* 2018-10-29 02:53
*To:* snowball0916 <mailto:snowball0...@163.com>; r-help
<mailto:r-help@r-project.org>
*Subject:* Re: [R] date and time data on x axis
Hello,
Maybe you could get some inspiration in the following code.
op <- par(mar = c(4, 0, 0, 0) + par("mar"))
plot(xdata, ydata, type = "o", xaxt = "n")
axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)
par(op)
The important part is the call axis.POSIXct, argument las = 2 and the
call to par is to make sure that the labels are visible.
Hope this helps,
Rui Barradas
Às 15:16 de 28/10/2018, snowball0916 escreveu:
 > Hi, guys
 > How do you guys deal with the date and time data on x axis?
 > I have some trouble with it. Could you help with this?
 >
 > =
 > Sample Data
 > =
 > The sample data look like this:
 >
 > 20181028_10:00:00 600
 > 20181028_10:00:01 500
 > 20181028_10:00:02 450
 > 20181028_10:00:03 660
 > ..
 >
 > =
 > My Code
 > =
 >
 > library(lubridate)
 > mydata <- read.table("e:/R_study/graph_test2.txt")
 > xdata <- ymd_hms(mydata$V1)
 > ydata <- mydata$V2
 > plot(xdata, ydata, type="o")
 >
 >
 > =
 > Questions:
 > =
 >
 > 1. Why my x axis does not show me the correct date time like
""2018-10-28 10:00:00 UTC" ?
 > 2. If my data is very huge(like data in every second and the data
has the whole day , even the whole month), how can I display the x
axis in a neat and clean way?
 >
 > Thanks very much.
 >
 >
 >
 >
 > [[alternative HTML version deleted]]
 >
 > __
 > R-help@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.
 >



__
R-help@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.


Re: [R] date and time data on x axis

2018-10-29 Thread MacQueen, Don via R-help
Here's an example of 24 hours of data at one second intervals.

npts <- 24*60*60

df <- data.frame(
 tm = seq( Sys.time(), by='1 sec', length=npts),
 yd = round(runif(npts),2)
 )

head(df)

with(df, plot(tm,yd))

The x axis appears to me to be displayed in a neat and clean way. I don't 
understand what the problem is.
(The data itself is illegible, but that's a different problem.)

The default axis may not be what you want, but it is neat and clean. To choose 
the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui 
mentioned.  help(axis.POSIXct) provides examples of actual use.

I prefer to do as much as possible with base R, so look at this example:

> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"

Therefore 
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)


par() is the function that sets graphical parameters. There are many graphical 
parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par 
)

This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.

Type the following commands
   par('mar')
   par()$mar## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')## to see that the margins have been changed

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" 
 wrote:

Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=
Sample Data
=
The sample data look like this:

20181028_10:00:00 600 
20181028_10:00:01 500 
20181028_10:00:02 450 
20181028_10:00:03 660
..

=
My Code
=

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=
Questions:
=

1. Why my x axis does not show me the correct date time like ""2018-10-28 
10:00:00 UTC" ?
2. If my data is very huge(like data in every second and the data has the 
whole day , even the whole month), how can I display the x axis in a neat and 
clean way?

Thanks very much.
 



[[alternative HTML version deleted]]

__
R-help@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.


__
R-help@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.


Re: [R] date and time data on x axis

2018-10-29 Thread snowball0916
Hi, Jim
Thanks very much, I will need to study your code, though.
Will large volume of data will affect the x axis display?
Thanks again. 
 


 
From: jim holtman
Date: 2018-10-29 05:53
To: snowball0916
CC: R mailing list
Subject: Re: [R] date and time data on x axis
You need to specify what the format of the date will be.  I am using
ggplot for the plot:
 
 
library(lubridate)
library(tidyverse)
mydata <- read.table(text = "time value
20181028_10:00:00 600
20181028_10:00:01 500
20181028_10:00:02 450
20181028_10:00:03 660", header = TRUE, as.is = TRUE)
 
mydata <- mutate(mydata,
 time = ymd_hms(time)
)
 
ggplot(mydata, aes(time, value)) +
  geom_point() +
  scale_x_datetime(date_labels = "%m/%d %H:%M:%S"
  ) +
  theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))
 
Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
 
Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
 
 
On Sun, Oct 28, 2018 at 11:23 AM snowball0916  wrote:
>
> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =
> Sample Data
> =
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ..
>
> =
> My Code
> =
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =
> Questions:
> =
>
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 
> 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the 
> whole day , even the whole month), how can I display the x axis in a neat and 
> clean way?
>
> Thanks very much.
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-10-29 Thread snowball0916
Hi, Rui
Thanks for your code, even though I'm not fully understand it.
And I am new to R, could you kindly help me with
-1). what's the nested usage stand for?
op <- par(mar = c(4, 0, 0, 0) + par("mar"))

-2). how can I get the axis.POSIXct usage or help?
I use help(axis) and help(POSIXct) and not found the actual use.

Thanks very much.






 
From: Rui Barradas
Date: 2018-10-29 02:53
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Hello,
 
Maybe you could get some inspiration in the following code.
 
 
op <- par(mar = c(4, 0, 0, 0) + par("mar"))
 
plot(xdata, ydata, type = "o", xaxt = "n")
axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)
 
par(op)
 
 
The important part is the call axis.POSIXct, argument las = 2 and the 
call to par is to make sure that the labels are visible.
 
 
Hope this helps,
 
Rui Barradas
 
 
Às 15:16 de 28/10/2018, snowball0916 escreveu:
> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
> 
> =
> Sample Data
> =
> The sample data look like this:
> 
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ..
> 
> =
> My Code
> =
> 
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
> 
> 
> =
> Questions:
> =
> 
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 
> 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the 
> whole day , even the whole month), how can I display the x axis in a neat and 
> clean way?
> 
> Thanks very much.
>   
> 
> 
> 
> [[alternative HTML version deleted]]
> 
> __
> R-help@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.
> 

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] date and time data on x axis

2018-10-28 Thread jim holtman
You need to specify what the format of the date will be.  I am using
ggplot for the plot:


library(lubridate)
library(tidyverse)
mydata <- read.table(text = "time value
20181028_10:00:00 600
20181028_10:00:01 500
20181028_10:00:02 450
20181028_10:00:03 660", header = TRUE, as.is = TRUE)

mydata <- mutate(mydata,
 time = ymd_hms(time)
)

ggplot(mydata, aes(time, value)) +
  geom_point() +
  scale_x_datetime(date_labels = "%m/%d %H:%M:%S"
  ) +
  theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Oct 28, 2018 at 11:23 AM snowball0916  wrote:
>
> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =
> Sample Data
> =
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ..
>
> =
> My Code
> =
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =
> Questions:
> =
>
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 
> 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the 
> whole day , even the whole month), how can I display the x axis in a neat and 
> clean way?
>
> Thanks very much.
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.


Re: [R] date and time data on x axis

2018-10-28 Thread Rui Barradas

Hello,

Maybe you could get some inspiration in the following code.


op <- par(mar = c(4, 0, 0, 0) + par("mar"))

plot(xdata, ydata, type = "o", xaxt = "n")
axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)

par(op)


The important part is the call axis.POSIXct, argument las = 2 and the 
call to par is to make sure that the labels are visible.



Hope this helps,

Rui Barradas


Às 15:16 de 28/10/2018, snowball0916 escreveu:

Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=
Sample Data
=
The sample data look like this:

20181028_10:00:00 600
20181028_10:00:01 500
20181028_10:00:02 450
20181028_10:00:03 660
..

=
My Code
=

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=
Questions:
=

1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 
UTC" ?
2. If my data is very huge(like data in every second and the data has the whole 
day , even the whole month), how can I display the x axis in a neat and clean 
way?

Thanks very much.
  




[[alternative HTML version deleted]]

__
R-help@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.



__
R-help@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.


[R] date and time data on x axis

2018-10-28 Thread snowball0916
Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=
Sample Data
=
The sample data look like this:

20181028_10:00:00 600 
20181028_10:00:01 500 
20181028_10:00:02 450 
20181028_10:00:03 660
..

=
My Code
=

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=
Questions:
=

1. Why my x axis does not show me the correct date time like ""2018-10-28 
10:00:00 UTC" ?
2. If my data is very huge(like data in every second and the data has the whole 
day , even the whole month), how can I display the x axis in a neat and clean 
way?

Thanks very much.
 



[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] R Date Time

2017-04-25 Thread Dirk Eddelbuettel

On 25 April 2017 at 18:05, Duncan Murdoch wrote:
| On 25/04/2017 5:41 PM, Dirk Eddelbuettel wrote:
| >
| > On 25 April 2017 at 16:04, Jeff Reichman wrote:
| > | R Users
| > |
| > | Having problems converting the following DTG into an R recognized 
date/time
| > | field
| > |
| > | 01-01-2016T14:02:23.325
| > |
| > | Would I separate it into a date field and time filed then put it back
| > | together???
| >
| > The anytime package (on CRAN) does this (and other date or datetime input
| > variants) without requiring a format:
| >
| >   R> library(anytime)
| >   R> anytime("01-01-2016T14:02:23.325")
| >   [1] "2016-01-01 14:02:23.325 CST"
| 
| How does it decide between MDY and DMY orderings in dates?  Doesn't 
| matter for this example, but it would for "01-02-2016T14:02:23.325"

See 

http://dirk.eddelbuettel.com/code/anytime.html

https://github.com/eddelbuettel/anytime

https://github.com/eddelbuettel/anytime/blob/master/src/anytime.cpp#L43-L106

for overview(s), some comments, notes and in particular the set of formats.

It has a strong preference for sane (ie ISO formats) but in the case of
ambiguity it (grudingly) prefers the (silly) US way:

R> anytime("01-02-2003")
[1] "2003-01-02 CST"
R>

But I try not to miss an opportunity that the format should really not be used.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-help@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.


Re: [R] R Date Time

2017-04-25 Thread Duncan Murdoch

On 25/04/2017 5:41 PM, Dirk Eddelbuettel wrote:


On 25 April 2017 at 16:04, Jeff Reichman wrote:
| R Users
|
| Having problems converting the following DTG into an R recognized date/time
| field
|
| 01-01-2016T14:02:23.325
|
| Would I separate it into a date field and time filed then put it back
| together???

The anytime package (on CRAN) does this (and other date or datetime input
variants) without requiring a format:

  R> library(anytime)
  R> anytime("01-01-2016T14:02:23.325")
  [1] "2016-01-01 14:02:23.325 CST"


How does it decide between MDY and DMY orderings in dates?  Doesn't 
matter for this example, but it would for "01-02-2016T14:02:23.325"


Duncan Murdoch

__
R-help@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.


Re: [R] R Date Time

2017-04-25 Thread Dirk Eddelbuettel

On 25 April 2017 at 16:04, Jeff Reichman wrote:
| R Users
| 
| Having problems converting the following DTG into an R recognized date/time
| field
| 
| 01-01-2016T14:02:23.325
| 
| Would I separate it into a date field and time filed then put it back
| together???

The anytime package (on CRAN) does this (and other date or datetime input
variants) without requiring a format:

  R> library(anytime)
  R> anytime("01-01-2016T14:02:23.325")
  [1] "2016-01-01 14:02:23.325 CST"
  R>

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
R-help@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.


Re: [R] R Date Time

2017-04-25 Thread William Dunlap via R-help
> z <- as.POSIXct("01-01-2016T14:02:23.325", format="%d-%m-%YT%H:%M:%OS")
> dput(z)
structure(1451685743.325, class = c("POSIXct", "POSIXt"), tzone = "")
> z
[1] "2016-01-01 14:02:23 PST"
> format(z, "%H:%M:%OS3 on %b %d, %Y")
[1] "14:02:23.325 on Jan 01, 2016"

(Don't separate the date and time parts because some times don't exist on
some days.)


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Apr 25, 2017 at 2:04 PM, Jeff Reichman 
wrote:

> R Users
>
>
>
> Having problems converting the following DTG into an R recognized date/time
> field
>
>
>
> 01-01-2016T14:02:23.325
>
>
>
> Would I separate it into a date field and time filed then put it back
> together???
>
>
>
> Jeff
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] R Date Time

2017-04-25 Thread Duncan Murdoch

On 25/04/2017 5:04 PM, Jeff Reichman wrote:

R Users



Having problems converting the following DTG into an R recognized date/time
field



01-01-2016T14:02:23.325



Would I separate it into a date field and time filed then put it back
together???



This appears to work (though I'm not sure whether you are using MDY or 
DMY; I used DMY):


strptime("01-01-2016T14:02:23.325", format="%d-%m-%YT%H:%M:%OS")

Duncan Murdoch

__
R-help@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.


[R] R Date Time

2017-04-25 Thread Jeff Reichman
R Users

 

Having problems converting the following DTG into an R recognized date/time
field

 

01-01-2016T14:02:23.325

 

Would I separate it into a date field and time filed then put it back
together???

 

Jeff


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Date operation Question in R

2017-03-31 Thread David Winsemius

> On Mar 30, 2017, at 3:16 PM, Thomas Petzoldt  wrote:
> 
> On 30.03.2017 23:34, Paul Bernal wrote:
>> Hello everyone,
>> 
>> Is there a way to use the function seq to generate a date sequence in
>> this kind of format: jan-2007?
> 
> format(seq(ISOdate(2017,1,1), ISOdate(2017,12,31), "months"), "%b-%Y")

But since the original one asked for a starting point of Sys.Date, on this 31st 
day of March, it might be useful to demonstrate that there are pifalls for the 
uninitiated useR. Note the many duplicate "months":

> format(seq(ISOdate(2017,1,31), ISOdate(2018,12,31), "months"), "%b-%Y")
 [1] "Jan-2017" "Mar-2017" "Mar-2017" "May-2017" "May-2017" "Jul-2017" 
"Jul-2017"
 [8] "Aug-2017" "Oct-2017" "Oct-2017" "Dec-2017" "Dec-2017" "Jan-2018" 
"Mar-2018"
[15] "Mar-2018" "May-2018" "May-2018" "Jul-2018" "Jul-2018" "Aug-2018" 
"Oct-2018"
[22] "Oct-2018" "Dec-2018" "Dec-2018"

-- 
David.
> 
>> 
>> Also, is there a way to change the Sys.Date() format to the one
>> mentioned above (jan-2007)?
> 
> format(Sys.Date(), "%b-%Y")
> 
> see ?strptime for details.
> 
> Thomas
> 
>> 
>> Thanks in advance for your valuable help,
>> 
>> Best regards,
>> 
>> Paul
> 
> __
> R-help@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.

David Winsemius
Alameda, CA, USA

__
R-help@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.


  1   2   3   4   5   >