Re: [R] a question on write.table

2015-09-28 Thread Giorgio Garziano
Try this:

X<-c("A","B","C","D","E")
Y<-c(0,1,2,3,4)

for (i in 0:3) {
  Y<-Y+i
  data<-data.frame(X,Y)
  fe.flag <- file.exists("test.csv")
  write.table(data, "test.csv", row.names = FALSE, col.names = !fe.flag, 
sep=";", append = fe.flag)
}




[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] a question on write.table

2015-09-28 Thread David Winsemius

On Sep 28, 2015, at 11:31 AM, Antonio Silva wrote:

> Dear R users
> 
> I want to write a file that contains several data frames generated in a loop
> ing.
> I also want the column names be written to file only when it is created in
> first loop.
> 
> In the example below, when I run each line separately without "for (i in
> ...) { }"  it works, but when I run the looping I get an error message
> 
> X<-c("A","B","C","D","E")
> Y<-c(0,1,2,3,4)
> 
> for (i in 0:3) {
> Y<-Y+i
> data<-data.frame(X,Y)
> ifelse(file.exists("test.csv"),
> write.table(data,"test.csv",row.names =
> FALSE,col.names=FALSE,sep=";",append=TRUE),
> write.table(data,"test.csv",row.names = FALSE,sep=";")

The basic problem is that you are using ifelse(test , cons , alt) when you 
should be using if(test){cons}else{alt}

`ifelse` will evaluate both cons and alt. You don't want that to happen.

-- 
David.


> )}
> 
> Error in ifelse(file.exists("test.csv"), write.table(data, "test.csv",  :
>  substituto tem comprimento zero
> Além disso: Warning message:
> In rep(yes, length.out = length(ans)) :
>  'x' is NULL so the result will be NULL
> 
> What is going wrong here? Thanks for any comments or suggestions.
> 
> All the best.
> 
> Antonio Olinto
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] a question on write.table

2015-09-28 Thread ruipbarradas
Hello,

ifelse is a vectorized version of if/else, you want the normal if/else.

if(file.exists(... etc ...)
    [...]
else
    [...]

Hope this helps,

Rui Barradas
 

Citando Antonio Silva :

> Dear R users
>
> I want to write a file that contains several data frames generated in a
> loop
> ing.
> I also want the column names be written to file only when it is created
in
> first loop.
>
> In the example below, when I run each line separately without "for (i in
> ...) { }"  it works, but when I run the looping I get an error message
>
> X<-c("A","B","C","D","E")
> Y<-c(0,1,2,3,4)
>
> for (i in 0:3) {
> Y<-Y+i
> data<-data.frame(X,Y)
> ifelse(file.exists("test.csv"),
> write.table(data,"test.csv",row.names =
> FALSE,col.names=FALSE,sep=";",append=TRUE),
> write.table(data,"test.csv",row.names = FALSE,sep=";")
> )}
>
> Error in ifelse(file.exists("test.csv"), write.table(data, "test.csv", 
:
> substituto tem comprimento zero
> Além disso: Warning message:
> In rep(yes, length.out = length(ans)) :
> 'x' is NULL so the result will be NULL
>
> What is going wrong here? Thanks for any comments or suggestions.
>
> All the best.
>
> Antonio Olinto
>
>         [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.htmland provide commented,
> minimal, self-contained, reproducible code.

 

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] a question on write.table

2015-09-28 Thread Antonio Silva
Thanks Giorgio, David and Rui

With the suggestions my problem was solved in different ways.

Best regards

Antonio


2015-09-28 15:52 GMT-03:00 Giorgio Garziano :

> Try this:
>
> X<-c("A","B","C","D","E")
> Y<-c(0,1,2,3,4)
>
> for (i in 0:3) {
>   Y<-Y+i
>   data<-data.frame(X,Y)
>   fe.flag <- file.exists("test.csv")
>   write.table(data, "test.csv", row.names = FALSE, col.names = !fe.flag,
> sep=";", append = fe.flag)
> }
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



-- 
Antônio Olinto Ávila da Silva
Biólogo / Oceanógrafo
Instituto de Pesca (Fisheries Institute)
São Paulo, Brasil

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.