koushiki sarkar
2014-Mar-18 02:32 UTC
[R] Beginner: How do I copy the results from a for loop in a csv file?
Hello I am using 2 for loops to find the difference between all rows of a matrix. I need to store it to a csv file. I have written this: for (i in 0:length(datamat)){ for (j in i+1:length(datamat)){ x<-datamat[i,]-datamat[j,]; y<-as.data.frama(x); write.csv(y, "dif.csv") }} datamat is the original datamatrix and dif is the file i want to copy the results to. However, when i open this file, I find it empty. What is it that I'm doing wrong? Also, can I store the results of this loop in another matrix? If then, how? I am new to R and not skilled in other programming languages. Any help is appreciated! Thank you [[alternative HTML version deleted]]
Peter Alspach
2014-Mar-18 04:10 UTC
[R] Beginner: How do I copy the results from a for loop in a csv file?
Tena koe What are you doing wrong? For one thing not supplying a simple reproducible example :-) Try: set.seed(12) (tempMat <- matrix(round(100*runif(12), 0), nrow=3)) [,1] [,2] [,3] [,4] [1,] 7 27 18 1 [2,] 82 17 64 39 [3,] 94 3 2 81 (diffMat <- apply(tempMat, 2, diff)) [,1] [,2] [,3] [,4] [1,] 75 -10 46 38 [2,] 12 -14 -62 42 and then write.csv(diffMat, 'diffMat.csv') HTH .... Peter Alspach -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of koushiki sarkar Sent: Tuesday, 18 March 2014 3:32 p.m. To: r-help at r-project.org Subject: [R] Beginner: How do I copy the results from a for loop in a csv file? Hello I am using 2 for loops to find the difference between all rows of a matrix. I need to store it to a csv file. I have written this: for (i in 0:length(datamat)){ for (j in i+1:length(datamat)){ x<-datamat[i,]-datamat[j,]; y<-as.data.frama(x); write.csv(y, "dif.csv") }} datamat is the original datamatrix and dif is the file i want to copy the results to. However, when i open this file, I find it empty. What is it that I'm doing wrong? Also, can I store the results of this loop in another matrix? If then, how? I am new to R and not skilled in other programming languages. Any help is appreciated! Thank you [[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. The contents of this e-mail are confidential and may be ...{{dropped:14}}
Bert Gunter
2014-Mar-18 04:30 UTC
[R] Beginner: How do I copy the results from a for loop in a csv file?
1. I would strongly recommend that you spend some time with one of the many R tutorials (An Introduction to R ships with R, but there are many more on the web) before attempting to write any further code or posting here. 2. Post in plaiin text, not HTML, as code can get scrambled in HTML. Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374(650) 467-7374(650) 467-7374(650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch ---------- Forwarded message ---------- From: koushiki sarkar <koushikisarkars at gmail.com> Date: Mon, Mar 17, 2014 at 7:32 PM Subject: [R] Beginner: How do I copy the results from a for loop in a csv file? To: r-help at r-project.org Hello I am using 2 for loops to find the difference between all rows of a matrix. I need to store it to a csv file. I have written this: for (i in 0:length(datamat)){ for (j in i+1:length(datamat)){ x<-datamat[i,]-datamat[j,]; y<-as.data.frama(x); write.csv(y, "dif.csv") }} datamat is the original datamatrix and dif is the file i want to copy the results to. However, when i open this file, I find it empty. What is it that I'm doing wrong? Also, can I store the results of this loop in another matrix? If then, how? I am new to R and not skilled in other programming languages. Any help is appreciated! Thank you [[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. Call Send SMS Add to Skype You'll need Skype CreditFree via Skype Call Send SMS Add to Skype You'll need Skype CreditFree via Skype Call Send SMS Add to Skype You'll need Skype CreditFree via Skype
David Winsemius
2014-Mar-18 06:13 UTC
[R] Beginner: How do I copy the results from a for loop in a csv file?
On Mar 17, 2014, at 7:32 PM, koushiki sarkar wrote:> Hello > I am using 2 for loops to find the difference between all rows of a matrix. > I need to store it to a csv file. I have written this: > for (i in 0:length(datamat)){ > for (j in i+1:length(datamat)){ > x<-datamat[i,]-datamat[j,]; > y<-as.data.frama(x); > write.csv(y, "dif.csv") > }} > > datamat is the original datamatrix and dif is the file i want to copy the > results to. > However, when i open this file, I find it empty. What is it that I'm doing > wrong?A) Not supplying a test case. B) Not realizing the need for parentheses to clarify intended operator precedence. C) Indexing by zero. D) Not realizing that the "length" of a matrix is the product of n and m E) Not reporting the complete error message(s). F) Misspelling `as.data.frame` G) Not using the 'file' argument to a write function. H) HTML I) Possibly: expecting files to be written with append=TRUE but not using the append argument.> Also, can I store the results of this loop in another matrix?Not really, at least not as stated. The "result" of a for-loop is NULL. You need to make any assignments to object names using assignments within the loop.> If > then, how? I am new to R and not skilled in other programming languages. > Any help is appreciated! > Thank you > > [[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.David Winsemius Alameda, CA, USA