Need help on "date"

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help on "date"

Arun.stat
Dear all,

I have a variable 'x' like that:

> x
[1] "2005-09-01"

Here, 2005 represents year, 09 month and 01 day.

Now I want to create three variables naming: y, m, and d such that:

y = 2005
m = 09
d = 01

can anyone tell me how to do that?

Regards,

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Frede Aakmann Tøgersen
One way (perhaps not the most efficient)

> as.Date("2005-09-01","%Y-%m-%d")
[1] "2005-09-01"
> format(as.Date("2005-09-01","%Y-%m-%d"),"%Y")
[1] "2005"
> format(as.Date("2005-09-01","%Y-%m-%d"),"%d")
[1] "01"
> format(as.Date("2005-09-01","%Y-%m-%d"),"%m")
[1] "09"
>


See ?DateTimeClasses.

Med venlig hilsen
Frede Aakmann Tøgersen
 

 

> -----Oprindelig meddelelse-----
> Fra: [hidden email]
> [mailto:[hidden email]] På vegne af Arun Kumar Saha
> Sendt: 18. september 2007 11:01
> Til: [hidden email]
> Emne: [R] Need help on "date"
>
> Dear all,
>
> I have a variable 'x' like that:
>
> > x
> [1] "2005-09-01"
>
> Here, 2005 represents year, 09 month and 01 day.
>
> Now I want to create three variables naming: y, m, and d such that:
>
> y = 2005
> m = 09
> d = 01
>
> can anyone tell me how to do that?
>
> Regards,
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

jholtman
In reply to this post by Arun.stat
Here is one way of doing it:

> x <- as.POSIXct("2005-09-01")
> x
[1] "2005-09-01 GMT"

> x.lt <- as.POSIXlt(x)
> x$mon+1
> x.lt$mon+1
[1] 9
> x.lt$year+1900
[1] 2005
> dput(x.lt)
structure(list(sec = 0, min = 0L, hour = 0L, mday = 1L, mon = 8L,
    year = 105L, wday = 4L, yday = 243L, isdst = 0L), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXt", "POSIXlt"), tzone = "GMT")
> x.lt$mday
[1] 1
>


On 9/18/07, Arun Kumar Saha <[hidden email]> wrote:

> Dear all,
>
> I have a variable 'x' like that:
>
> > x
> [1] "2005-09-01"
>
> Here, 2005 represents year, 09 month and 01 day.
>
> Now I want to create three variables naming: y, m, and d such that:
>
> y = 2005
> m = 09
> d = 01
>
> can anyone tell me how to do that?
>
> Regards,
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>


--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Prof Brian Ripley
In reply to this post by Frede Aakmann Tøgersen
On Tue, 18 Sep 2007, Frede Aakmann Tøgersen wrote:

> One way (perhaps not the most efficient)
>
>> as.Date("2005-09-01","%Y-%m-%d")
> [1] "2005-09-01"
>> format(as.Date("2005-09-01","%Y-%m-%d"),"%Y")
> [1] "2005"
>> format(as.Date("2005-09-01","%Y-%m-%d"),"%d")
> [1] "01"
>> format(as.Date("2005-09-01","%Y-%m-%d"),"%m")
> [1] "09"
It's pretty efficient, but should you want numeric (rather than character)
answers

> zz <- strptime("2005-09-01","%Y-%m-%d")
> zz$year + 1900
[1] 2005
> zz$mon + 1
[1] 9
> zz$mday
[1] 1

(I'm not sure why the POSIX people chose inconsistent origins, but they
did.)

>>
>
>
> See ?DateTimeClasses.
>
> Med venlig hilsen
> Frede Aakmann Tøgersen
>
>
>
>
>> -----Oprindelig meddelelse-----
>> Fra: [hidden email]
>> [mailto:[hidden email]] På vegne af Arun Kumar Saha
>> Sendt: 18. september 2007 11:01
>> Til: [hidden email]
>> Emne: [R] Need help on "date"
>>
>> Dear all,
>>
>> I have a variable 'x' like that:
>>
>>> x
>> [1] "2005-09-01"
>>
>> Here, 2005 represents year, 09 month and 01 day.
>>
>> Now I want to create three variables naming: y, m, and d such that:
>>
>> y = 2005
>> m = 09
>> d = 01
>>
>> can anyone tell me how to do that?
>>
>> Regards,
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> 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.
>>
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>
--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Gabor Grothendieck
In reply to this post by Arun.stat
There have been a few solutions already but I thought I would add one
that uses chron:

> library(chron)
> attach(month.day.year(chron(unclass(as.Date("2005-09-01")))))
> year
[1] 2005
> month
[1] 9
> day
[1] 1

or perhaps cleaner:

library(chron)
with(month.day.year(chron(unclass(as.Date("2006-09-01")))), {
   ... computations involving variables month, day and year ...
}

See R News 4/1 help desk article for info on dates.  Particularly the table
at the end.
On 9/18/07, Arun Kumar Saha <[hidden email]> wrote:

> Dear all,
>
> I have a variable 'x' like that:
>
> > x
> [1] "2005-09-01"
>
> Here, 2005 represents year, 09 month and 01 day.
>
> Now I want to create three variables naming: y, m, and d such that:
>
> y = 2005
> m = 09
> d = 01
>
> can anyone tell me how to do that?
>
> Regards,
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Jeffrey Robert Spies
In reply to this post by Arun.stat
And one using regular expressions:

x <- "2005-09-01"
pattern <- '([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})'
y <- sub(pattern, '\\1', x)
m <- sub(pattern, '\\2', x)
d <- sub(pattern, '\\3', x)

-- Jeff.

On Sep 18, 2007, at 5:00 AM, Arun Kumar Saha wrote:

> Dear all,
>
> I have a variable 'x' like that:
>
>> x
> [1] "2005-09-01"
>
> Here, 2005 represents year, 09 month and 01 day.
>
> Now I want to create three variables naming: y, m, and d such that:
>
> y = 2005
> m = 09
> d = 01
>
> can anyone tell me how to do that?
>
> Regards,
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Arun.stat
Dear Jeffrey,

Your syntax looks very extraordinary to me. I would be very happy if you can
explain this notation.

Regards,

On 9/18/07, Jeffrey Robert Spies <[hidden email]> wrote:

>
> And one using regular expressions:
>
> x <- "2005-09-01"
> pattern <- '([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})'
> y <- sub(pattern, '\\1', x)
> m <- sub(pattern, '\\2', x)
> d <- sub(pattern, '\\3', x)
>
> -- Jeff.
>
> On Sep 18, 2007, at 5:00 AM, Arun Kumar Saha wrote:
>
> > Dear all,
> >
> > I have a variable 'x' like that:
> >
> >> x
> > [1] "2005-09-01"
> >
> > Here, 2005 represents year, 09 month and 01 day.
> >
> > Now I want to create three variables naming: y, m, and d such that:
> >
> > y = 2005
> > m = 09
> > d = 01
> >
> > can anyone tell me how to do that?
> >
> > Regards,
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > 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]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Jeffrey Robert Spies
Sub uses POSIX-extended regular expressions.  It searches for the  
first argument, the pattern, and replaces it with the second argument  
in the variable defined by the third argument.  [[:digit:]] is a  
match-any-digit operator; it matches the characters 0-9.  The {#} is  
the interval operator, where what's inside the bracket's is a count.  
So [[:digit:]]{4} means match 4 digits.  All together, ([[:digit:]]
{4})-([[:digit:]]{2})-([[:digit:]]{2}) means "Match 4 digits followed  
by a dash followed by 2 digits followed by a dash followed by 2 digits.
By surrounding pieces of the search pattern in parentheses, we create  
back-references, which can be used in the replacement (second  
argument) like variables, \\1 to \\9, in the order that they appear  
in the pattern. When we replace the pattern with '\\1', that means  
return what is in the first set of parentheses, or the first four  
digits before a dash before two digits before a dash before another  
two digits.

Note: most of the time, we'd use single slashes to escape a character  
(i.e. \1), but R needs double slashes (i.e. \\1).

If you're interested in regular expressions, this site is quite  
useful: http://www.cs.utah.edu/dept/old/texinfo/regex/regex_toc.html.

Make sense?

Jeff.

On Sep 18, 2007, at 10:46 AM, Arun Kumar Saha wrote:

> Dear Jeffrey,
>
> Your syntax looks very extraordinary to me. I would be very happy  
> if you can explain this notation.
>
> Regards,
>
> On 9/18/07, Jeffrey Robert Spies <[hidden email]> wrote:And one  
> using regular expressions:
>
> x <- "2005-09-01"
> pattern <- '([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})'
> y <- sub(pattern, '\\1', x)
> m <- sub(pattern, '\\2', x)
> d <- sub(pattern, '\\3', x)
>
> -- Jeff.
>
> On Sep 18, 2007, at 5:00 AM, Arun Kumar Saha wrote:
>
> > Dear all,
> >
> > I have a variable 'x' like that:
> >
> >> x
> > [1] "2005-09-01"
> >
> > Here, 2005 represents year, 09 month and 01 day.
> >
> > Now I want to create three variables naming: y, m, and d such that:
> >
> > y = 2005
> > m = 09
> > d = 01
> >
> > can anyone tell me how to do that?
> >
> > Regards,
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > 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.
>
>
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Gabor Grothendieck
If you are interested in regular expressions you may also be
interested in a solution using the gsubfn package.  Here x
is the input character string, re is Jeffrey's regular expression
and strapply applies the regular expression to x calling the function
which is represented in formula notation using the free variables
year, month and day as the arguments.  backref = -3 says
only pass the 3 backreferences, i.e. the matched portion within
parens, and not the entire string.  The function is set up to take
a vector input for x but since we only have one element we use [[1]].

library(gsubfn)

x <- "2005-09-01"
re <- "([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})"

strapply(x, re, ~ c(year = year, month = month, day = day), backref = -3)[[1]]


On 9/19/07, Jeffrey Robert Spies <[hidden email]> wrote:

> Sub uses POSIX-extended regular expressions.  It searches for the
> first argument, the pattern, and replaces it with the second argument
> in the variable defined by the third argument.  [[:digit:]] is a
> match-any-digit operator; it matches the characters 0-9.  The {#} is
> the interval operator, where what's inside the bracket's is a count.
> So [[:digit:]]{4} means match 4 digits.  All together, ([[:digit:]]
> {4})-([[:digit:]]{2})-([[:digit:]]{2}) means "Match 4 digits followed
> by a dash followed by 2 digits followed by a dash followed by 2 digits.
> By surrounding pieces of the search pattern in parentheses, we create
> back-references, which can be used in the replacement (second
> argument) like variables, \\1 to \\9, in the order that they appear
> in the pattern. When we replace the pattern with '\\1', that means
> return what is in the first set of parentheses, or the first four
> digits before a dash before two digits before a dash before another
> two digits.
>
> Note: most of the time, we'd use single slashes to escape a character
> (i.e. \1), but R needs double slashes (i.e. \\1).
>
> If you're interested in regular expressions, this site is quite
> useful: http://www.cs.utah.edu/dept/old/texinfo/regex/regex_toc.html.
>
> Make sense?
>
> Jeff.
>
> On Sep 18, 2007, at 10:46 AM, Arun Kumar Saha wrote:
>
> > Dear Jeffrey,
> >
> > Your syntax looks very extraordinary to me. I would be very happy
> > if you can explain this notation.
> >
> > Regards,
> >
> > On 9/18/07, Jeffrey Robert Spies <[hidden email]> wrote:And one
> > using regular expressions:
> >
> > x <- "2005-09-01"
> > pattern <- '([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})'
> > y <- sub(pattern, '\\1', x)
> > m <- sub(pattern, '\\2', x)
> > d <- sub(pattern, '\\3', x)
> >
> > -- Jeff.
> >
> > On Sep 18, 2007, at 5:00 AM, Arun Kumar Saha wrote:
> >
> > > Dear all,
> > >
> > > I have a variable 'x' like that:
> > >
> > >> x
> > > [1] "2005-09-01"
> > >
> > > Here, 2005 represents year, 09 month and 01 day.
> > >
> > > Now I want to create three variables naming: y, m, and d such that:
> > >
> > > y = 2005
> > > m = 09
> > > d = 01
> > >
> > > can anyone tell me how to do that?
> > >
> > > Regards,
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list
> > > 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.
> >
> >
> >
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Arun.stat
In reply to this post by Arun.stat
This mail is continuation of my previous one. i have some raw data from
Excel which was carried to R:

data = read.delim(file="clipboard", header=T)
> data
       Date Price
1  09/01/05   365
2  09/02/05   360
3  09/03/05   360
4  09/05/05   370
5  09/06/05   370
6  09/08/05   365
7  09/09/05   365
8  09/10/05   365
9  09/12/05   365
10 09/13/05   360
11 09/14/05   360
12 09/15/05   360

and, using the input from R-help I exctracted day, month, and year, from
"Date"

year = as.numeric(format(as.Date(data[,1],"%m/%d/%y"),"%Y"))
month = as.numeric(format(as.Date(data[,1],"%m/%d/%y"),"%m"))
day = as.numeric(format(as.Date(data[,1],"%m/%d/%y"),"%d"))

Now I want to create a date-variable and put it in actual data:

library(date)
data1 = cbind(mdy.date(month, day, year), data[,-1])
> data1
       [,1] [,2]
 [1,] 16680  365
 [2,] 16681  360
 [3,] 16682  360
 [4,] 16684  370
 [5,] 16685  370
 [6,] 16687  365
 [7,] 16688  365
 [8,] 16689  365
 [9,] 16691  365
[10,] 16692  360
[11,] 16693  360
[12,] 16694  360

However this is not that thing what I wanted, first column has been jumbled,
it is not in actual date format.

Can anyone tell me what should i do here?

Regards,






On 9/18/07, Arun Kumar Saha <[hidden email]> wrote:

>
> Dear all,
>
> I have a variable 'x' like that:
>
> > x
> [1] "2005-09-01"
>
> Here, 2005 represents year, 09 month and 01 day.
>
> Now I want to create three variables naming: y, m, and d such that:
>
> y = 2005
> m = 09
> d = 01
>
> can anyone tell me how to do that?
>
> Regards,
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on "date"

Gabor Grothendieck
Since this is a time series you might want to look at the zoo package:

Lines <- "Date Price
09/01/05   365
09/02/05   360
09/03/05   360
09/05/05   370
09/06/05   370
09/08/05   365
09/09/05   365
09/10/05   365
09/12/05   365
09/13/05   360
09/14/05   360
09/15/05   360
"

library(zoo)
# replace with
#    z <- read.zoo("myfile.dat", header = TRUE,format = "%m/%d/%Y")
z <- read.zoo(textConnection(Lines), header = TRUE, format = "%m/%d/%Y")
plot(z)

vignette("zoo") # info on zoo
vignette("zoo-quickref") # more info on zoo



On 9/20/07, Arun Kumar Saha <[hidden email]> wrote:

> This mail is continuation of my previous one. i have some raw data from
> Excel which was carried to R:
>
> data = read.delim(file="clipboard", header=T)
> > data
>       Date Price
> 1  09/01/05   365
> 2  09/02/05   360
> 3  09/03/05   360
> 4  09/05/05   370
> 5  09/06/05   370
> 6  09/08/05   365
> 7  09/09/05   365
> 8  09/10/05   365
> 9  09/12/05   365
> 10 09/13/05   360
> 11 09/14/05   360
> 12 09/15/05   360
>
> and, using the input from R-help I exctracted day, month, and year, from
> "Date"
>
> year = as.numeric(format(as.Date(data[,1],"%m/%d/%y"),"%Y"))
> month = as.numeric(format(as.Date(data[,1],"%m/%d/%y"),"%m"))
> day = as.numeric(format(as.Date(data[,1],"%m/%d/%y"),"%d"))
>
> Now I want to create a date-variable and put it in actual data:
>
> library(date)
> data1 = cbind(mdy.date(month, day, year), data[,-1])
> > data1
>       [,1] [,2]
>  [1,] 16680  365
>  [2,] 16681  360
>  [3,] 16682  360
>  [4,] 16684  370
>  [5,] 16685  370
>  [6,] 16687  365
>  [7,] 16688  365
>  [8,] 16689  365
>  [9,] 16691  365
> [10,] 16692  360
> [11,] 16693  360
> [12,] 16694  360
>
> However this is not that thing what I wanted, first column has been jumbled,
> it is not in actual date format.
>
> Can anyone tell me what should i do here?
>
> Regards,
>
>
>
>
>
>
> On 9/18/07, Arun Kumar Saha <[hidden email]> wrote:
> >
> > Dear all,
> >
> > I have a variable 'x' like that:
> >
> > > x
> > [1] "2005-09-01"
> >
> > Here, 2005 represents year, 09 month and 01 day.
> >
> > Now I want to create three variables naming: y, m, and d such that:
> >
> > y = 2005
> > m = 09
> > d = 01
> >
> > can anyone tell me how to do that?
> >
> > Regards,
> >
> >
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.