Dear all: I am having some problems to use the function "sink()". Basically I am doing a loop over two files which contain unit-root variables. Then on a loop, I extract every i element of both files to create an object called z. If z meets some requirements, then I perform a unit root test (ADF test), otherwise not. As this process is repeated several times, for each i I want to get the summary of the ADF test on a common file. For that I use the function "sink()". My code runs fine, but I do not get anything written on the text file where my results are supposed to be saved. The code is below setwd("C:\\Users\\Sergio René\\Dropbox\\R") library("urca") P1<-read.csv("2R_EQ_P_R1_500.csv") P2<-read.csv("2R_EQ_P_R2_500.csv") d<-(1:1000) sink ("ADF_results_b_1.txt") for (i in seq(d)) { z.1<-P1[i]*-1-P2[i]*-1 if (all(z.1<=0)) {r=1} else {if (all(z.1>=0)) {r=1} else {r=2}} if (r==1) {ADF<-ur.df(ts(z.1), lags=1, type='drift')} if (r==1) {summary(ADF)} } sink() Any suggestion of what I might be doing wroong? best regards, Sergio René [[alternative HTML version deleted]]
Hi, Inside a loop, you must explicitly wrap your summary() command and anything else from which you expect output in a print() command. Sarah 2011/10/11 Sergio Ren? Araujo Enciso <araujo.enciso at gmail.com>:> Dear all: > > I am having some problems to use the function "sink()". Basically I am doing > a loop over two files which contain unit-root variables. Then on a loop, I > extract every i element of both files to create an object called z. If z > meets some requirements, then I perform a unit root test (ADF test), > otherwise not. As this process is repeated several times, for each i I want > to get the summary of the ADF test on a common file. For that I use the > function "sink()". My code runs fine, but I do not get anything written on > the text file where my results are supposed to be saved. The code is below > > setwd("C:\\Users\\Sergio Ren?\\Dropbox\\R") > > library("urca") > > P1<-read.csv("2R_EQ_P_R1_500.csv") > P2<-read.csv("2R_EQ_P_R2_500.csv") > > d<-(1:1000) > sink ("ADF_results_b_1.txt") > > for (i in seq(d)) > { > z.1<-P1[i]*-1-P2[i]*-1 > if (all(z.1<=0)) {r=1} else {if (all(z.1>=0)) {r=1} else {r=2}} > if (r==1) {ADF<-ur.df(ts(z.1), lags=1, type='drift')} > if (r==1) {summary(ADF)} > } > sink() > > Any suggestion of what I might be doing wroong? > > best regards, > > Sergio Ren? >-- Sarah Goslee http://www.functionaldiversity.org
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Oct-11 17:10 UTC
[R] Help to write to a file
Untested, does adding a print() around summary() get it done? Michael On Oct 11, 2011, at 1:03 PM, Sergio Ren? Araujo Enciso <araujo.enciso at gmail.com> wrote:> Dear all: > > I am having some problems to use the function "sink()". Basically I am doing > a loop over two files which contain unit-root variables. Then on a loop, I > extract every i element of both files to create an object called z. If z > meets some requirements, then I perform a unit root test (ADF test), > otherwise not. As this process is repeated several times, for each i I want > to get the summary of the ADF test on a common file. For that I use the > function "sink()". My code runs fine, but I do not get anything written on > the text file where my results are supposed to be saved. The code is below > > setwd("C:\\Users\\Sergio Ren?\\Dropbox\\R") > > library("urca") > > P1<-read.csv("2R_EQ_P_R1_500.csv") > P2<-read.csv("2R_EQ_P_R2_500.csv") > > d<-(1:1000) > sink ("ADF_results_b_1.txt") > > for (i in seq(d)) > { > z.1<-P1[i]*-1-P2[i]*-1 > if (all(z.1<=0)) {r=1} else {if (all(z.1>=0)) {r=1} else {r=2}} > if (r==1) {ADF<-ur.df(ts(z.1), lags=1, type='drift')} > if (r==1) {summary(ADF)} > } > sink() > > Any suggestion of what I might be doing wroong? > > best regards, > > Sergio Ren? > > [[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.
On 11/10/2011 1:03 PM, Sergio Ren? Araujo Enciso wrote:> Dear all: > > I am having some problems to use the function "sink()". Basically I am doing > a loop over two files which contain unit-root variables. Then on a loop, I > extract every i element of both files to create an object called z. If z > meets some requirements, then I perform a unit root test (ADF test), > otherwise not. As this process is repeated several times, for each i I want > to get the summary of the ADF test on a common file. For that I use the > function "sink()". My code runs fine, but I do not get anything written on > the text file where my results are supposed to be saved. The code is below > > setwd("C:\\Users\\Sergio Ren?\\Dropbox\\R") > > library("urca") > > P1<-read.csv("2R_EQ_P_R1_500.csv") > P2<-read.csv("2R_EQ_P_R2_500.csv") > > d<-(1:1000) > sink ("ADF_results_b_1.txt") > > for (i in seq(d)) > { > z.1<-P1[i]*-1-P2[i]*-1 > if (all(z.1<=0)) {r=1} else {if (all(z.1>=0)) {r=1} else {r=2}} > if (r==1) {ADF<-ur.df(ts(z.1), lags=1, type='drift')} > if (r==1) {summary(ADF)} > } > sink() > > Any suggestion of what I might be doing wroong?You aren't printing anything. In a loop, you need to call print() explicitly; only the last value of an expression auto-prints. Duncan Murdoch
On Oct 11, 2011, at 1:03 PM, Sergio Ren? Araujo Enciso wrote:> Dear all: > > I am having some problems to use the function "sink()". Basically I > am doing > a loop over two files which contain unit-root variables. Then on a > loop, I > extract every i element of both files to create an object called z. > If z > meets some requirements, then I perform a unit root test (ADF test), > otherwise not. As this process is repeated several times, for each i > I want > to get the summary of the ADF test on a common file. For that I use > the > function "sink()". My code runs fine, but I do not get anything > written on > the text file where my results are supposed to be saved. The code is > below > > setwd("C:\\Users\\Sergio Ren?\\Dropbox\\R") > > library("urca") > > P1<-read.csv("2R_EQ_P_R1_500.csv") > P2<-read.csv("2R_EQ_P_R2_500.csv") > > d<-(1:1000) > sink ("ADF_results_b_1.txt") > > for (i in seq(d)) > { > z.1<-P1[i]*-1-P2[i]*-1 > if (all(z.1<=0)) {r=1} else {if (all(z.1>=0)) {r=1} else {r=2}} > if (r==1) {ADF<-ur.df(ts(z.1), lags=1, type='drift')} > if (r==1) {summary(ADF)}You may need to print() that summary-object inside the for-function. > summary(a) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 1.75 2.50 2.50 3.25 4.00 > sink("test.txt") > for(i in 1) summary(a) > sink() # No test.txt file created > sink("test2.txt") > for(i in 1) print( summary(a) ) > sink() # The expected file created This relates to the FAQ about similar puzzling behavior with plotting lattice , grid or ggplot objects.> } > sink() > > Any suggestion of what I might be doing wroong? > > best regards, > > Sergio Ren?David Winsemius, MD West Hartford, CT
Ok, I see my mistake, just did as you suggest and works. Thanks for the answer people Best, Sergio Rné El 11 de octubre de 2011 19:03, Sergio René Araujo Enciso < araujo.enciso@gmail.com> escribió:> Dear all: > > I am having some problems to use the function "sink()". Basically I am > doing a loop over two files which contain unit-root variables. Then on a > loop, I extract every i element of both files to create an object called z. > If z meets some requirements, then I perform a unit root test (ADF test), > otherwise not. As this process is repeated several times, for each i I want > to get the summary of the ADF test on a common file. For that I use the > function "sink()". My code runs fine, but I do not get anything written on > the text file where my results are supposed to be saved. The code is below > > setwd("C:\\Users\\Sergio René\\Dropbox\\R") > > library("urca") > > P1<-read.csv("2R_EQ_P_R1_500.csv") > P2<-read.csv("2R_EQ_P_R2_500.csv") > > d<-(1:1000) > sink ("ADF_results_b_1.txt") > > for (i in seq(d)) > { > z.1<-P1[i]*-1-P2[i]*-1 > if (all(z.1<=0)) {r=1} else {if (all(z.1>=0)) {r=1} else {r=2}} > if (r==1) {ADF<-ur.df(ts(z.1), lags=1, type='drift')} > if (r==1) {summary(ADF)} > } > sink() > > Any suggestion of what I might be doing wroong? > > best regards, > > Sergio René >[[alternative HTML version deleted]]