Hi Sarah,
Thanks for getting back to me.Here is an example of my data:SampleData <-
structure(list(id = 1:7, value = c(5813L, 8706L, 4049L, 5877L,?? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ?1375L, 2223L, 3423L), date = structure(c(4L, 3L, 2L,
1L, 7L,?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? 6L, 5L), .Label = c("05/12/11", "06/05/11",
"07-Dec-11",?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "19-Dec-11",
"01/22/2011", "10/19/2011", "31/12/2011"? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ),
class = "factor")), .Names = c("id", "value",
"date"), row.names = c(NA,?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -7L), class =
"data.frame")SampleData
Thanks for your help:).
?
On Wednesday, July 29, 2015 1:50 PM, Sarah Goslee <sarah.goslee at
gmail.com> wrote:
On Wed, Jul 29, 2015 at 2:45 PM, farnoosh sheikhi via R-help
<r-help at r-project.org> wrote:>? Hi Arun,
> Hope all is well with you. I have a data with a column for date.The date
format is mixed. There are date values with Month/Day/Year format and values
with Day/Month/Year format.I don't know how to unify it.I really appreciate
your help.Thanks.
You sent this to the R-help list, not just to Arun, so I'm assuming
this is an R question. The best way to get help is to provide a sample
of your data using dput() and to clearly specify what you would like
as the result - "unify" is a bit vague. paste(x,
collapse="") could be
considered unification, after all.
Sarah
--
Sarah Goslee
http://www.functionaldiversity.org
[[alternative HTML version deleted]]
I'm assuming you actually want the date column to be character, not factor:
SampleData <- structure(list(id = 1:7, value = c(5813L, 8706L, 4049L, 5877L,
1375L, 2223L, 3423L), date = c("19-Dec-11", "07-Dec-11",
"06/05/11",
"05/12/11", "31/12/2011", "10/19/2011",
"01/22/2011")), .Names = c("id",
"value", "date"), row.names = c(NA, -7L), class =
"data.frame")
id value date
1 1 5813 19-Dec-11
2 2 8706 07-Dec-11
3 3 4049 06/05/11
4 4 5877 05/12/11
5 5 1375 31/12/2011
6 6 2223 10/19/2011
7 7 3423 01/22/2011
Given this assemblage of dates, there is a lot of ambiguity. The first
two are clear, but is the third in May or June? Is the fourth May or
December?
I had hoped that the number of digits in the year provided a clue, but
#5 is clearly dd/mm/yyyy while #7 is mm/dd/yyyy
Or actually, #3 could be yy/mm/dd too.
I don't see a way to programmatically solve your problem, because
there is no clear way to parse all of these dates because of the
ambiguity.
You could get some of them by checking for values outside the bounds
of legitimate month numbers, or just try it and discard the values
that give NA results:> as.Date("01/22/2011", "%m/%d/%Y")
[1] "2011-01-22"> as.Date("01/22/2011", "%d/%m/%Y")
[1] NA
But you need more information to figure out the rest.
Sarah
On Wed, Jul 29, 2015 at 5:15 PM, farnoosh sheikhi <farnoosh_81 at
yahoo.com> wrote:> Hi Sarah,
>
> Thanks for getting back to me.
> Here is an example of my data:
> SampleData <- structure(list(id = 1:7, value = c(5813L, 8706L, 4049L,
5877L,
> 1375L, 2223L, 3423L), date
> structure(c(4L, 3L, 2L, 1L, 7L,
>
> 6L, 5L), .Label = c("05/12/11", "06/05/11",
"07-Dec-11",
>
> "19-Dec-11", "01/22/2011", "10/19/2011",
"31/12/2011"
>
> ), class = "factor")), .Names = c("id",
"value", "date"), row.names = c(NA,
>
> -7L), class = "data.frame")
> SampleData
>
> Thanks for your help:).
>
>
>
>
>
>
> On Wednesday, July 29, 2015 1:50 PM, Sarah Goslee <sarah.goslee at
gmail.com>
> wrote:
>
>
> On Wed, Jul 29, 2015 at 2:45 PM, farnoosh sheikhi via R-help
>
> <r-help at r-project.org> wrote:
>> Hi Arun,
>> Hope all is well with you. I have a data with a column for date.The
date
>> format is mixed. There are date values with Month/Day/Year format and
values
>> with Day/Month/Year format.I don't know how to unify it.I really
appreciate
>> your help.Thanks.
>
>
> You sent this to the R-help list, not just to Arun, so I'm assuming
> this is an R question. The best way to get help is to provide a sample
> of your data using dput() and to clearly specify what you would like
> as the result - "unify" is a bit vague. paste(x,
collapse="") could be
> considered unification, after all.
>
> Sarah
Hi
I wonder if it is easier to convert the dates to character format and then
reformat using gsub or the like
str(SampleData)
SampleData$date <- as.character(SampleData$date)
str(SampleData)
as.Date(
ifelse(nchar(SampleData[,"date"]) == 9,
as.Date(SampleData[,"date"], format = "%d-%b-%y"),
ifelse(nchar(SampleData[,"date"]) == 8,
as.Date(SampleData[,"date"], format = "%d/%m/%y"),
ifelse(as.numeric(substr(SampleData[,"date"],1,2)) >
12,
as.Date(SampleData[,"date"], format =
"%d/%m/%Y"),
as.Date(SampleData[,"date"], format =
"%m/%d/%Y")) )), origin = as.Date("1970-01-01"))
Beware of the American format in months jan feb mar oct nov -- will need more
conditions to be imposed
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of farnoosh
sheikhi via R-help
Sent: Thursday, 30 July 2015 07:16
To: Sarah Goslee
Cc: R. Help
Subject: Re: [R] Mixed Date Formats
Hi Sarah,
Thanks for getting back to me.Here is an example of my data:SampleData <-
structure(list(id = 1:7, value = c(5813L, 8706L, 4049L, 5877L,
1375L, 2223L, 3423L), date = structure(c(4L, 3L, 2L, 1L, 7L,
6L, 5L), .Label = c("05/12/11", "06/05/11",
"07-Dec-11",
"19-Dec-11", "01/22/2011", "10/19/2011",
"31/12/2011"
), class = "factor")), .Names = c("id", "value",
"date"), row.names = c(NA,
-7L), class = "data.frame")SampleData
Thanks for your help:).
On Wednesday, July 29, 2015 1:50 PM, Sarah Goslee <sarah.goslee at
gmail.com> wrote:
On Wed, Jul 29, 2015 at 2:45 PM, farnoosh sheikhi via R-help
<r-help at r-project.org> wrote:> Hi Arun,
> Hope all is well with you. I have a data with a column for date.The date
format is mixed. There are date values with Month/Day/Year format and values
with Day/Month/Year format.I don't know how to unify it.I really appreciate
your help.Thanks.
You sent this to the R-help list, not just to Arun, so I'm assuming
this is an R question. The best way to get help is to provide a sample
of your data using dput() and to clearly specify what you would like
as the result - "unify" is a bit vague. paste(x,
collapse="") could be
considered unification, after all.
Sarah
--
Sarah Goslee
http://www.functionaldiversity.org
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.