HI,
If you want to extract the month as in the format you mentioned in the original
post:
Month<-format(dat1[,1],format="%b")
head(Month)
#[1] "Jan" "Jan" "Jan" "Jan" "Jan" "Jan"
library(ISOweek)
Week<-date2ISOweek(dat1[,1])
Week1<-gsub(".*\\-(.*)\\-.*","\\1",Week)
head(Week1)
#[1] "W01" "W01" "W01" "W01" "W01" "W02"
A.K.
________________________________
From: Fares Said <[email protected]>
To: arun <[email protected]>
Sent: Friday, January 4, 2013 2:10 PM
Subject: RE: Can you help me please
Thank you so much,
That what I am look for. I really appreciated your help.
is there a function I can extract the month and the week from the date column?
Thanks
> Date: Fri, 4 Jan 2013 10:53:23 -0800
> From: [email protected]
> Subject: Re: Can you help me please
> To: [email protected]
> CC: [email protected]
>
> HI Fares,
>
> Sorry, that I misunderstand your question.
> Probably, this works for you.
> date1<-
> seq.Date(as.Date("1jan2003",format="%d%b%Y"),as.Date("1jan2013",format="%d%b%Y"),by="day")
> length(date1)
> #[1] 3654
> set.seed(51)
> donation<-sample(1000:3000000,3654,replace=FALSE)
> dat1<-data.frame(date1,donation)
> library(chron)
> nrow(dat1[is.weekend(dat1[,1]),])
> #[1] 1044
> set.seed(15)
> dat1[,2][is.weekend(dat1[,1])]<-sample(1:500,1044,replace=TRUE)
> head(dat1)
> # date1 donation
> #1 2003-01-01 2328151
> #2 2003-01-02 602210
> #3 2003-01-03 895550
> #4 2003-01-04 302
> #5 2003-01-05 98
> #6 2003-01-06 2503433
> nrow(dat1)
> #[1] 3654
> str(dat1)
> #'data.frame': 3654 obs. of 2 variables:
> # $ date1 : Date, format: "2003-01-01" "2003-01-02" ...
> # $ donation: int 2328151 602210 895550 302 98 2503433 2254394 1218622
> 2913082 337881 ...
>
>
>
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Fares Said <[email protected]>
> To: arun <[email protected]>
> Cc:
> Sent: Friday, January 4, 2013 1:38 PM
> Subject: Re: Can you help me please
>
> Sorry Arun,
>
> I don't have any columns I need to generate them both with certain criteria.
> First column is the date from jan2003 until jan2013. And the second columns
> is number of donations. But make sure that the number is greater than 1000
> for weekdays and less than 500 for weekends. And after you create the date
> column I would like to know how I can format that column.
>
> I hope this is clear now and thank u so much.
>
> Fares
>
> Sent from my iPhone
>
> On 2013-01-04, at 13:26, "arun" <[email protected]> wrote:
>
> > HI Fares,
> >
> > I thought you had the data available for both the columns, and only wanted
> > to convert the "date" column to as.Date(date). How do I know about the
> > number of donations from Jan-2003 to Jan 2013? Do you have that data
> > column? I assume that the first column is missing. Is that right?
> > A.K.
> >
> >
> >
> >
> >
> >
> > ________________________________
> > From: Fares Said <[email protected]>
> > To: arun <[email protected]>
> > Sent: Friday, January 4, 2013 1:23 PM
> > Subject: RE: Can you help me please
> >
> >
> >
> > Hi A.k,
> >
> > no that is not what I am looking for.
> > I need to generate a date column starting from Jan-2003 until Jan 2013
> >then another column with number of donations
> >
> > Thanks
> >
> >
> >> Date: Fri, 4 Jan 2013 10:18:17 -0800
> >> From: [email protected]
> >> Subject: Re: Can you help me please
> >> To: [email protected]
> >>
> >> Hi Fares,
> >>
> >> YOur post says that you need "time series". xts is extensible time series.
> >> If you wanted to create a time column;
> >>
> >> dat1$date<-as.Date(dat1$date,format="%d%b%Y")
> >> str(dat1)
> >> #'data.frame': 7 obs. of 2 variables:
> >> # $ date : Date, format: "2003-01-03" "2003-01-04" ...
> >> # $ donation: int 20235 25655 225860 289658 243889 244338 243889
> >> dat1
> >> # date donation
> >> #1 2003-01-03 20235
> >> #2 2003-01-04 25655
> >> #3 2003-01-05 225860
> >> #4 2003-01-06 289658
> >> #5 2003-01-07 243889
> >> #6 2003-01-08 244338
> >> #7 2003-01-09 243889
> >> Is this what you wanted? Else, you could email with the output format.
> >> A.K.
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> ________________________________
> >> From: Fares Said <[email protected]>
> >> To: arun <[email protected]>
> >> Sent: Friday, January 4, 2013 1:12 PM
> >> Subject: RE: Can you help me please
> >>
> >>
> >>
> >> Hi A.k,
> >>
> >> Thank you but this doesn't help me. I need to know how to create a time
> >> column. Can you help me with that please
> >>
> >> Thanks
> >>
> >>
> >>> Date: Fri, 4 Jan 2013 09:58:31 -0800
> >>> From: [email protected]
> >>> Subject: Re: Can you help me please
> >>> To: [email protected]
> >>> CC: [email protected]
> >>>
> >>>
> >>>
> >>> HI Fares,
> >>>
> >>> You could try this:
> >>> dat1<- read.table(text="
> >>> date donation
> >>> 3jan2003 20235
> >>> 4jan2003 25655
> >>> 5jan2003 225860
> >>> 6jan2003 289658
> >>> 7jan2003 243889
> >>> 8jan2003 244338
> >>> 9jan2003 243889
> >>> ",sep="",header=TRUE,stringsAsFactors=FALSE)
> >>>
> >>>
> >>> The post is not very specific as to what you need. I hope this works for
> >>> you.
> >>>
> >>>
> >>> library(xts)
> >>> dat2<-xts(dat1[,2],order.by=as.Date(dat1[,1],format="%d%b%Y"))
> >>> dat2
> >>> # [,1]
> >>> #2003-01-03 20235
> >>> #2003-01-04 25655
> >>> #2003-01-05 225860
> >>> #2003-01-06 289658
> >>> #2003-01-07 243889
> >>> #2003-01-08 244338
> >>> #2003-01-09 243889
> >>>
> >>> str(dat2)
> >>> #An ‘xts’ object from 2003-01-03 to 2003-01-09 containing:
> >>> # Data: int [1:7, 1] 20235 25655 225860 289658 243889 244338 243889
> >>> # Indexed by objects of class: [Date] TZ:
> >>> # xts Attributes:
> >>> #List of 2
> >>> # $ tclass: chr "Date"
> >>> #$ tzone : chr ""
> >>> plot(dat2)
> >>>
> >>> A.K.
> >>> ________________________________
> >>> From: Fares Said <[email protected]>
> >>> To: arun <[email protected]>
> >>> Sent: Friday, January 4, 2013 9:16 AM
> >>> Subject: Can you help me please
> >>>
> >>>
> >>>
> >>> Hi A.K,
> >>>
> >>> I am not sure if you have looked at this question,
> >>>
> >>> If not can you please look
> >>>
> >>> http://r.789695.n4.nabble.com/Generate-time-series-data-td4654589.html
> >>>
> >>> Thanks
>
______________________________________________
[email protected] 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.