Hi, R experts. I am a new user of R and trying to learn this program. I have a problem. Here is the code. d<-as.Date(c("2000/01/03","2000/01/05","2000/01/19","2000/01/28")) r<-rnorm(4) da<-data.frame(d,r) a<-as.Date("01/01/2000","%d/%m/%Y") b<-as.Date("30/01/2000","%d/%m/%Y") ab<-seq(a,b,by=1) c<-format(ab,"%a") date<-data.frame(ab,c) date<-subset(date,c!="Sun") date<-subset(date,c!="Sat") Here I have 2 data frame. da ------------- d r 1 2000-01-03 1.2105865 2 2000-01-05 -0.8962776 3 2000-01-19 -1.0438936 4 2000-01-28 2.1329387 --------------------------- date -------------- ab c 3 2000-01-03 Mon 4 2000-01-04 Tue 5 2000-01-05 Wed 6 2000-01-06 Thu 7 2000-01-07 Fri 10 2000-01-10 Mon 11 2000-01-11 Tue 12 2000-01-12 Wed 13 2000-01-13 Thu 14 2000-01-14 Fri 17 2000-01-17 Mon 18 2000-01-18 Tue 19 2000-01-19 Wed 20 2000-01-20 Thu 21 2000-01-21 Fri 24 2000-01-24 Mon 25 2000-01-25 Tue 26 2000-01-26 Wed 27 2000-01-27 Thu 28 2000-01-28 Fri --------------- In data frame-"DA"- I have return(r) and date In data frame-"Date"- where I have date and day. Now I need to create a data frame where returns will be conditional on date and rest will be zero. ------------------- like this --------------------- ab c r hd 3 2000-01-03 Mon 1.2105865 0 4 2000-01-04 Tue 0 1 5 2000-01-05 Wed 0 1 6 2000-01-06 Thu 0 1 7 2000-01-07 Fri 0 1 --------------------------------->From this I can figureout the hoildays and then put each holidays equal to1. Please help me. Thanking you saikat -- View this message in context: http://www.nabble.com/How-can-we-creat-conditional-data-frame-tp16491207p16491207.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Hi, R experts. I am a new user of R and trying to learn this program. I have a problem. Here is the code. d<-as.Date(c("2000/01/03","2000/01/05","2000/01/19","2000/01/28")) r<-rnorm(4) da<-data.frame(d,r) a<-as.Date("01/01/2000","%d/%m/%Y") b<-as.Date("30/01/2000","%d/%m/%Y") ab<-seq(a,b,by=1) c<-format(ab,"%a") date<-data.frame(ab,c) date<-subset(date,c!="Sun") date<-subset(date,c!="Sat") Here I have 2 data frame. da ------------- d r 1 2000-01-03 1.2105865 2 2000-01-05 -0.8962776 3 2000-01-19 -1.0438936 4 2000-01-28 2.1329387 --------------------------- date -------------- ab c 3 2000-01-03 Mon 4 2000-01-04 Tue 5 2000-01-05 Wed 6 2000-01-06 Thu 7 2000-01-07 Fri 10 2000-01-10 Mon 11 2000-01-11 Tue 12 2000-01-12 Wed 13 2000-01-13 Thu 14 2000-01-14 Fri 17 2000-01-17 Mon 18 2000-01-18 Tue 19 2000-01-19 Wed 20 2000-01-20 Thu 21 2000-01-21 Fri 24 2000-01-24 Mon 25 2000-01-25 Tue 26 2000-01-26 Wed 27 2000-01-27 Thu 28 2000-01-28 Fri --------------- In data frame-"DA"- I have return(r) and date In data frame-"Date"- where I have date and day. Now I need to create a data frame where returns will be conditional on date and rest will be zero. ------------------- like this --------------------- ab c r hd 3 2000-01-03 Mon 1.2105865 0 4 2000-01-04 Tue 0 1 5 2000-01-05 Wed 0 1 6 2000-01-06 Thu 0 1 7 2000-01-07 Fri 0 1 --------------------------------->From this I can figureout the hoildays and then put each holidays equal to1. Please help me. Thanking you saikat -- View this message in context: http://www.nabble.com/How-can-we-creat-conditional-data-frame-tp16491208p16491208.html Sent from the R help mailing list archive at Nabble.com.
Have a look at the zoo package. After reading in the data its only 2 lines of code. Note that zoo objects can be numeric or factor but not both so we have represented the day of the week as 1 for Mon, etc. in the final object. You could convert it to a data frame at the end if you really need to combine factors or strings and numerics in one object. Lines.r <- "d r 2000-01-03 1.2105865 2000-01-05 -0.8962776 2000-01-19 -1.0438936 2000-01-28 2.1329387 " Lines.date <- "ab c 2000-01-03 Mon 2000-01-04 Tue 2000-01-05 Wed 2000-01-06 Thu 2000-01-07 Fri 2000-01-10 Mon 2000-01-11 Tue 2000-01-12 Wed 2000-01-13 Thu 2000-01-14 Fri 2000-01-17 Mon 2000-01-18 Tue 2000-01-19 Wed 2000-01-20 Thu 2000-01-21 Fri 2000-01-24 Mon 2000-01-25 Tue 2000-01-26 Wed 2000-01-27 Thu 2000-01-28 Fri " library(zoo) # replace textConnection(...) with your file name, e.g. # read.zoo("myfile.dat", header = TRUE) r.z <- read.zoo(textConnection(Lines.da), header = TRUE) date.z <- read.zoo(textConnection(Lines.date), header = TRUE) tmp.z <- merge(r.z, zoo(, time(date.z)), fill = 0) z <- merge(r = tmp.z, weekday = as.numeric(format(time(z, "%w"))), h = !z) For more info: library(zoo) vignette("zoo") vignette("zoo-quickref") vignette("zoo-faq") On Fri, Apr 4, 2008 at 7:49 AM, saikat sarkar <dipa_sanyal at yahoo.com> wrote:> > Hi, > > R experts. I am a new user of R and trying to learn this program. > > I have a problem. Here is the code. > > d<-as.Date(c("2000/01/03","2000/01/05","2000/01/19","2000/01/28")) > r<-rnorm(4) > da<-data.frame(d,r) > > a<-as.Date("01/01/2000","%d/%m/%Y") > b<-as.Date("30/01/2000","%d/%m/%Y") > ab<-seq(a,b,by=1) > c<-format(ab,"%a") > date<-data.frame(ab,c) > date<-subset(date,c!="Sun") > date<-subset(date,c!="Sat") > > Here I have 2 data frame. > > da > ------------- > d r > 1 2000-01-03 1.2105865 > 2 2000-01-05 -0.8962776 > 3 2000-01-19 -1.0438936 > 4 2000-01-28 2.1329387 > > --------------------------- > date > -------------- > ab c > 3 2000-01-03 Mon > 4 2000-01-04 Tue > 5 2000-01-05 Wed > 6 2000-01-06 Thu > 7 2000-01-07 Fri > 10 2000-01-10 Mon > 11 2000-01-11 Tue > 12 2000-01-12 Wed > 13 2000-01-13 Thu > 14 2000-01-14 Fri > 17 2000-01-17 Mon > 18 2000-01-18 Tue > 19 2000-01-19 Wed > 20 2000-01-20 Thu > 21 2000-01-21 Fri > 24 2000-01-24 Mon > 25 2000-01-25 Tue > 26 2000-01-26 Wed > 27 2000-01-27 Thu > 28 2000-01-28 Fri > > --------------- > > In data frame-"DA"- I have return(r) and date > In data frame-"Date"- where I have date and day. > > Now I need to create a data frame where returns will be conditional on date > and rest will be zero. > > ------------------- > like this > --------------------- > > ab c r hd > 3 2000-01-03 Mon 1.2105865 0 > 4 2000-01-04 Tue 0 1 > 5 2000-01-05 Wed 0 1 > 6 2000-01-06 Thu 0 1 > 7 2000-01-07 Fri 0 1 > > --------------------------------- > > >From this I can figureout the hoildays and then put each holidays equal to > 1. > > > Please help me. > > Thanking you > > saikat > > > > -- > View this message in context: http://www.nabble.com/How-can-we-creat-conditional-data-frame-tp16491208p16491208.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
The last line should have been: z <- merge(r = tmp.z, weekday = as.numeric(format(time(date.z), "%w")), h = !tmp.z) On Fri, Apr 4, 2008 at 8:25 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Have a look at the zoo package. After reading > in the data its only 2 lines of code. > > Note that zoo objects can be numeric or > factor but not both so we have represented > the day of the week as 1 for Mon, etc. > in the final object. You could convert it > to a data frame at the end if you really > need to combine factors or strings and > numerics in one object. > > > Lines.r <- "d r > 2000-01-03 1.2105865 > 2000-01-05 -0.8962776 > 2000-01-19 -1.0438936 > 2000-01-28 2.1329387 > " > > Lines.date <- "ab c > 2000-01-03 Mon > 2000-01-04 Tue > 2000-01-05 Wed > 2000-01-06 Thu > 2000-01-07 Fri > 2000-01-10 Mon > 2000-01-11 Tue > 2000-01-12 Wed > 2000-01-13 Thu > 2000-01-14 Fri > 2000-01-17 Mon > 2000-01-18 Tue > 2000-01-19 Wed > 2000-01-20 Thu > 2000-01-21 Fri > 2000-01-24 Mon > 2000-01-25 Tue > 2000-01-26 Wed > 2000-01-27 Thu > 2000-01-28 Fri > " > > library(zoo) > # replace textConnection(...) with your file name, e.g. > # read.zoo("myfile.dat", header = TRUE) > r.z <- read.zoo(textConnection(Lines.da), header = TRUE) > date.z <- read.zoo(textConnection(Lines.date), header = TRUE) > > tmp.z <- merge(r.z, zoo(, time(date.z)), fill = 0) > z <- merge(r = tmp.z, weekday = as.numeric(format(time(z, "%w"))), h = !z) > > For more info: > > library(zoo) > vignette("zoo") > vignette("zoo-quickref") > vignette("zoo-faq") > > > On Fri, Apr 4, 2008 at 7:49 AM, saikat sarkar <dipa_sanyal at yahoo.com> wrote: > > > > Hi, > > > > R experts. I am a new user of R and trying to learn this program. > > > > I have a problem. Here is the code. > > > > d<-as.Date(c("2000/01/03","2000/01/05","2000/01/19","2000/01/28")) > > r<-rnorm(4) > > da<-data.frame(d,r) > > > > a<-as.Date("01/01/2000","%d/%m/%Y") > > b<-as.Date("30/01/2000","%d/%m/%Y") > > ab<-seq(a,b,by=1) > > c<-format(ab,"%a") > > date<-data.frame(ab,c) > > date<-subset(date,c!="Sun") > > date<-subset(date,c!="Sat") > > > > Here I have 2 data frame. > > > > da > > ------------- > > d r > > 1 2000-01-03 1.2105865 > > 2 2000-01-05 -0.8962776 > > 3 2000-01-19 -1.0438936 > > 4 2000-01-28 2.1329387 > > > > --------------------------- > > date > > -------------- > > ab c > > 3 2000-01-03 Mon > > 4 2000-01-04 Tue > > 5 2000-01-05 Wed > > 6 2000-01-06 Thu > > 7 2000-01-07 Fri > > 10 2000-01-10 Mon > > 11 2000-01-11 Tue > > 12 2000-01-12 Wed > > 13 2000-01-13 Thu > > 14 2000-01-14 Fri > > 17 2000-01-17 Mon > > 18 2000-01-18 Tue > > 19 2000-01-19 Wed > > 20 2000-01-20 Thu > > 21 2000-01-21 Fri > > 24 2000-01-24 Mon > > 25 2000-01-25 Tue > > 26 2000-01-26 Wed > > 27 2000-01-27 Thu > > 28 2000-01-28 Fri > > > > --------------- > > > > In data frame-"DA"- I have return(r) and date > > In data frame-"Date"- where I have date and day. > > > > Now I need to create a data frame where returns will be conditional on date > > and rest will be zero. > > > > ------------------- > > like this > > --------------------- > > > > ab c r hd > > 3 2000-01-03 Mon 1.2105865 0 > > 4 2000-01-04 Tue 0 1 > > 5 2000-01-05 Wed 0 1 > > 6 2000-01-06 Thu 0 1 > > 7 2000-01-07 Fri 0 1 > > > > --------------------------------- > > > > >From this I can figureout the hoildays and then put each holidays equal to > > 1. > > > > > > Please help me. > > > > Thanking you > > > > saikat > > > > > > > > -- > > View this message in context: http://www.nabble.com/How-can-we-creat-conditional-data-frame-tp16491208p16491208.html > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > 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 the solution data<-read.table("d:/ftse.txt",header=TRUE) wDay<-as.Date(data$date,"%d/%m/%Y") data<-data.frame(data[,-1]) price<-data$ftse100 data<-data.frame(price,wDay) data[1:10,] a<-as.Date("03/01/1989","%d/%m/%Y") b<-as.Date("31/01/2007","%d/%m/%Y") ab<-seq(a,b,by=1) c<-format(ab,"%a") date<-data.frame(ab,c) date<-subset(date,c!="Sun") date<-subset(date,c!="Sat") res <- merge(date, data, by.x = "ab", by.y="wDay", all=T) res$h <- 0 res$h[is.na(res$price)] <- 1 res$price[is.na(res$price)] <- 0 h<-res$h[-1] h1<-c(h,0) final.data<- data.frame(res$price,h1) final.data<-final.data[final.data$res.price>0,] saikat sarkar wrote:> > Hi, > > R experts. I am a new user of R and trying to learn this program. > > I have a problem. Here is the code. > > d<-as.Date(c("2000/01/03","2000/01/05","2000/01/19","2000/01/28")) > r<-rnorm(4) > da<-data.frame(d,r) > > a<-as.Date("01/01/2000","%d/%m/%Y") > b<-as.Date("30/01/2000","%d/%m/%Y") > ab<-seq(a,b,by=1) > c<-format(ab,"%a") > date<-data.frame(ab,c) > date<-subset(date,c!="Sun") > date<-subset(date,c!="Sat") > > Here I have 2 data frame. > > da > ------------- > d r > 1 2000-01-03 1.2105865 > 2 2000-01-05 -0.8962776 > 3 2000-01-19 -1.0438936 > 4 2000-01-28 2.1329387 > > --------------------------- > date > -------------- > ab c > 3 2000-01-03 Mon > 4 2000-01-04 Tue > 5 2000-01-05 Wed > 6 2000-01-06 Thu > 7 2000-01-07 Fri > 10 2000-01-10 Mon > 11 2000-01-11 Tue > 12 2000-01-12 Wed > 13 2000-01-13 Thu > 14 2000-01-14 Fri > 17 2000-01-17 Mon > 18 2000-01-18 Tue > 19 2000-01-19 Wed > 20 2000-01-20 Thu > 21 2000-01-21 Fri > 24 2000-01-24 Mon > 25 2000-01-25 Tue > 26 2000-01-26 Wed > 27 2000-01-27 Thu > 28 2000-01-28 Fri > > --------------- > > In data frame-"DA"- I have return(r) and date > In data frame-"Date"- where I have date and day. > > Now I need to create a data frame where returns will be conditional on > date and rest will be zero. > > ------------------- > like this > --------------------- > > ab c r hd > 3 2000-01-03 Mon 1.2105865 0 > 4 2000-01-04 Tue 0 1 > 5 2000-01-05 Wed 0 1 > 6 2000-01-06 Thu 0 1 > 7 2000-01-07 Fri 0 1 > > --------------------------------- > > From this I can figureout the hoildays and then put each holidays equal to > 1. > > > Please help me. > > Thanking you > > saikat > > > >-- View this message in context: http://www.nabble.com/How-can-we-creat-conditional-data-frame-tp16491208p16495146.html Sent from the R help mailing list archive at Nabble.com.