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]]
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: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] P? vegne af Arun Kumar Saha > Sendt: 18. september 2007 11:01 > Til: r-help at stat.math.ethz.ch > 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]] > > ______________________________________________ > R-help at r-project.org 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. >
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 <arun.kumar.saha at gmail.com> 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]] > > ______________________________________________ > R-help at r-project.org 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?
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 <arun.kumar.saha at gmail.com> 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]] > > ______________________________________________ > R-help at r-project.org 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. >
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]] > > ______________________________________________ > R-help at r-project.org 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.
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)> dataDate 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 <arun.kumar.saha@gmail.com> 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]]