Daniel Wu
2011-Jan-17 14:59 UTC
[R] to append a column to a data frame, has I use loop/if in my case?
days=Sys.Date()-1:70 price=abs(rnorm(70)) regular=rep(c(0,0,0,0,1,0,1,0,0,1),c(7,7,7,7,7,7,7,7,7,7)) y=data.frame(cbind(days,price,regular)) y is like days price regular 1 14990 0.16149463 0 2 14989 1.69519358 0 3 14988 1.57821998 0 4 14987 0.47614311 0 5 14986 0.87016180 0 6 14985 2.55679229 0 7 14984 0.89753533 0 the output I want: have another column appended to y, whose value is the max price in the recent 2 **regular** weeks. So if the current row is today, then get the max price of the past 14 days (including today) if the last 2 week are regular weeks, if one of the last 2 weeks is not regular week, then I need to go back further to find the max price, as I need the max price for the last 2 **regular** weeks. How can I do that? Or I have to use loop/if to do it? BTW, why the days is like 14990,14989, after cbind(days,price,regular)? before the cbind, days is like the format "2010-12-23". [[alternative HTML version deleted]]
Petr PIKAL
2011-Jan-18 14:26 UTC
[R] Odp: to append a column to a data frame, has I use loop/if in my case?
Hi r-help-bounces at r-project.org napsal dne 17.01.2011 15:59:37:> days=Sys.Date()-1:70 > price=abs(rnorm(70)) > regular=rep(c(0,0,0,0,1,0,1,0,0,1),c(7,7,7,7,7,7,7,7,7,7)) > y=data.frame(cbind(days,price,regular)) > > > y is like > days price regular > 1 14990 0.16149463 0 > 2 14989 1.69519358 0 > 3 14988 1.57821998 0 > 4 14987 0.47614311 0 > 5 14986 0.87016180 0 > 6 14985 2.55679229 0 > 7 14984 0.89753533 0 > > > the output I want: > have another column appended to y, whose value is the max price in therecent> 2 **regular** weeks.I have no idea what is regular week. Seems to me that you seek some rolling maxima for which I suggest to consult package zoo and its function rollapply.> So if the current row is today, then get the max price of the past 14days> (including today) if the last 2 week are regular weeks, if one of thelast 2> weeks is not regular week, then I need to go back further to find themax> price, as I need the max price for the last 2 **regular** weeks. How canI do> that? Or I have to use loop/if to do it? > > BTW, why the days is like 14990,14989, after cbind(days,price,regular)?before> the cbind, days is like the format "2010-12-23". > [[alternative HTML version deleted]]cbind binds together three vectors and makes a matrix. Matrix can have only values of only one type. Therefore it strips off days to underlying numeric representation. If you want to know more about R objects and their properties you could read R intro.> str(cbind(days,price,regular))num [1:70, 1:3] 14991 14990 14989 14988 14987 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:3] "days" "price" "regular" Regards Petr> > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Joshua Ulrich
2011-Jan-18 15:22 UTC
[R] to append a column to a data frame, has I use loop/if in my case?
Please do not cross post: http://stackoverflow.com/q/4720076/271616 At the minimum, it would be polite to respond here with the answer you accepted on Stack Overflow. -- Joshua Ulrich ?| ?FOSS Trading: www.fosstrading.com On Mon, Jan 17, 2011 at 8:59 AM, Daniel Wu <daniel_wu_r at 163.com> wrote:> days=Sys.Date()-1:70 > price=abs(rnorm(70)) > regular=rep(c(0,0,0,0,1,0,1,0,0,1),c(7,7,7,7,7,7,7,7,7,7)) > y=data.frame(cbind(days,price,regular)) > > > y is like > ? ?days ? ? ?price regular > 1 ?14990 0.16149463 ? ? ? 0 > 2 ?14989 1.69519358 ? ? ? 0 > 3 ?14988 1.57821998 ? ? ? 0 > 4 ?14987 0.47614311 ? ? ? 0 > 5 ?14986 0.87016180 ? ? ? 0 > 6 ?14985 2.55679229 ? ? ? 0 > 7 ?14984 0.89753533 ? ? ? 0 > > > the output I want: > have another column appended to y, whose value is the max price in the recent 2 **regular** weeks. > So if the current row is today, then get the max price of the past 14 days (including today) if the last 2 week are regular weeks, ?if one of the last 2 weeks is not regular week, then I need to go back further to find the max price, as I need the max price for the last 2 **regular** weeks. How can I do that? Or I have to use loop/if to do it? > > > > > BTW, why the days is like 14990,14989, after cbind(days,price,regular)? before the cbind, days is like the format "2010-12-23". > ? ? ? ?[[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
2011-Jan-18 15:47 UTC
[R] to append a column to a data frame, has I use loop/if in my case?
Here is how you should be creating your dataframe so that each element has the proper mode:> days=Sys.Date()-1:70 > price=abs(rnorm(70)) > regular=rep(c(0,0,0,0,1,0,1,0,0,1),c(7,7,7,7,7,7,7,7,7,7)) > y=data.frame(cbind(days,price,regular)) > str(y)'data.frame': 70 obs. of 3 variables: $ days : num 14991 14990 14989 14988 14987 ... # notice is it numeric $ price : num 0.626 0.184 0.836 1.595 0.33 ... $ regular: num 0 0 0 0 0 0 0 0 0 0 ...> y <- data.frame(days, price, regular) > str(y)'data.frame': 70 obs. of 3 variables: $ days :Class 'Date' num [1:70] 14991 14990 14989 14988 14987 ...# here it is 'Date' $ price : num 0.626 0.184 0.836 1.595 0.33 ... $ regular: num 0 0 0 0 0 0 0 0 0 0 ...> ydays price regular 1 2011-01-17 0.62645381 0 2 2011-01-16 0.18364332 0 3 2011-01-15 0.83562861 0 4 2011-01-14 1.59528080 0 5 2011-01-13 0.32950777 0 6 2011-01-12 0.82046838 0 7 2011-01-11 0.48742905 0 8 2011-01-10 0.73832471 0 On Mon, Jan 17, 2011 at 9:59 AM, Daniel Wu <daniel_wu_r at 163.com> wrote:> days=Sys.Date()-1:70 > price=abs(rnorm(70)) > regular=rep(c(0,0,0,0,1,0,1,0,0,1),c(7,7,7,7,7,7,7,7,7,7)) > y=data.frame(cbind(days,price,regular)) > > > y is like > ? ?days ? ? ?price regular > 1 ?14990 0.16149463 ? ? ? 0 > 2 ?14989 1.69519358 ? ? ? 0 > 3 ?14988 1.57821998 ? ? ? 0 > 4 ?14987 0.47614311 ? ? ? 0 > 5 ?14986 0.87016180 ? ? ? 0 > 6 ?14985 2.55679229 ? ? ? 0 > 7 ?14984 0.89753533 ? ? ? 0 > > > the output I want: > have another column appended to y, whose value is the max price in the recent 2 **regular** weeks. > So if the current row is today, then get the max price of the past 14 days (including today) if the last 2 week are regular weeks, ?if one of the last 2 weeks is not regular week, then I need to go back further to find the max price, as I need the max price for the last 2 **regular** weeks. How can I do that? Or I have to use loop/if to do it? > > > > > BTW, why the days is like 14990,14989, after cbind(days,price,regular)? before the cbind, days is like the format "2010-12-23". > ? ? ? ?[[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 Data Munger Guru What is the problem that you are trying to solve?