Hi, I read a dataset into R. It looks like this:> rawlistx1 x2 x3 2 12 64 5 12 42 5 9 42 18 14 535 26 23 751 Now I want to append some values of zero to let it look like this: x1 x2 x3 2 12 64 5 12 42 5 9 42 18 14 535 26 23 751 0 0 0 0 0 0 0 0 0 0 0 0 How to do this? Thanks, Yu-Ling Wu __________________________________________________ Do You Yahoo!? Yahoo! Auctions - Buy the things you want at great prices. http://auctions.yahoo.com/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Yu-Ling Wu <yuling5 at yahoo.com> writes:> Hi, > > I read a dataset into R. It looks like this: > > > rawlist > x1 x2 x3 > 2 12 64 > 5 12 42 > 5 9 42 > 18 14 535 > 26 23 751 > > Now I want to append some values of zero to let it > look like this: > > x1 x2 x3 > 2 12 64 > 5 12 42 > 5 9 42 > 18 14 535 > 26 23 751 > 0 0 0 > 0 0 0 > 0 0 0 > 0 0 0 > > How to do this?Something along the lines of zeros <- as.data.frame(matrix(0,4,3)) names(zeros) <- names(rawlist) rbind(rawlist, zeros) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /''_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
rbind(as.matrix(rawlist),matrix(0,ncol=3,nrow=4)) will do it, although depending on what you''re doing next you may want to turn the result back into a dataframe after attaching the zeros. I can''t quite figure out the logic that says why rbind(rawlist,matrix(0,ncol=3,nrow=4)) (i.e., leaving rawlist as a dataframe) leads to a data frame with only one row of zeros appended. Ben Bolker On Fri, 26 Jan 2001, Yu-Ling Wu wrote:> Hi, > > I read a dataset into R. It looks like this: > > > rawlist > x1 x2 x3 > 2 12 64 > 5 12 42 > 5 9 42 > 18 14 535 > 26 23 751 > > Now I want to append some values of zero to let it > look like this: > > x1 x2 x3 > 2 12 64 > 5 12 42 > 5 9 42 > 18 14 535 > 26 23 751 > 0 0 0 > 0 0 0 > 0 0 0 > 0 0 0 > > How to do this? > > Thanks, > Yu-Ling Wu > > > __________________________________________________ > Do You Yahoo!? > Yahoo! Auctions - Buy the things you want at great prices. > http://auctions.yahoo.com/ > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Yu-Ling Wu wrote:> > Hi, > > I read a dataset into R. It looks like this: > > > rawlist > x1 x2 x3 > 2 12 64 > 5 12 42 > 5 9 42 > 18 14 535 > 26 23 751 > > Now I want to append some values of zero to let it > look like this: > > x1 x2 x3 > 2 12 64 > 5 12 42 > 5 9 42 > 18 14 535 > 26 23 751 > 0 0 0 > 0 0 0 > 0 0 0 > 0 0 0 > > How to do this?One possibility (assuming that rawlist is a data frame) is: rawlist <- sapply(rawlist,function(x)c(x,rep(0,4))) -- ----------------------------------------------------------------- Pierre Kleiber Email: pkleiber at honlab.nmfs.hawaii.edu Fishery Biologist Tel: 808 983-5399/737-7544 NOAA FISHERIES - Honolulu Laboratory Fax: 808 983-2902 2570 Dole St., Honolulu, HI 96822-2396 ----------------------------------------------------------------- "God could have told Moses about galaxies and mitochondria and all. But behold... It was good enough for government work." ----------------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._