On Sun, 2005-02-13 at 20:53 -0800, Cuichang Zhao wrote: > Hello, > right now, i have a program to collect data into a table. right now, my table > is > table1 <- data.frame(trial = NA, x = NA, y = NA)
One often uses the term 'table' as in tabulate or cross-tabulate discrete values. > for each time when i want to add data into my data, i have to copy data of > table into an array for each column, and then i add new data into my array, > then i copy my array into the table one column by one column. For example > temptrial <- table1$trial; > temptrial <- c(temptrial, "1, 2"); > tempx <- table1$x; > tempx <- c(tempx, "1, 2"); > tempy <- table1$y; > tempy <- c(tempy, "1, 2"); > > table1 <- data.frame(trial = temptrial, x = tempx, y = tempy); I am not exactly sure what you are trying to do, but here is one way to achieve the same results. df <- data.frame(trial=NA, x=NA, y=NA) rbind( df, "1, 2" ) trial x y 1 <NA> <NA> <NA> 2 1, 2 1, 2 1, 2 Note that R will recycle an element such as "1, 2". See if the following example helps set.seed(1) output <- matrix(nc=2, nr=5) for(i in 1:5){ x <- sample(0:1, 100, replace=T) # simulate data output[i, ] <- c( table(x) ) } output [,1] [,2] [1,] 52 48 [2,] 46 54 [3,] 62 38 [4,] 57 43 [5,] 53 47 You can do the same for a cross-table where you flatten a table into a vector using c() before inserting it into a the pre-fabricated output matrix. > the way i am doing makes my program very slow, because i need to copy the > data from one variable to another variable all the time. however, it should > not be necessary, so i am just wasting the memory, can any one tell me how > can i make my program run faster without copying temparary data all the time. > > Also, i have to write this table in a file using "cat", however, "cat" does > not support the table format, so i can't do it. could any one also tell me > how can i write my data into an outfile. What do you mean by cat() does not support table format ? Have you tried sink() or write.table() ? Here are some examples sink(file="R_sink.txt") table(x, y) sink("") write.table( table(x, y), row.names=FALSE, file="R_table.txt" ) > Thank you so much > > C-Ming > > Feb 13, 2005 > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html