Hello! Hope everyone is doing as well as possible. I have a question about write.csv. I thought that the append argument would permit adding another data frame with the same column names. Here is my example: date1 <- as.Date("2020-03-09") wday <- weekdays(date1) x1 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) x1 date day lev y 1 2020-03-09 Monday 1 -0.09543049 2 2020-03-09 Monday 2 0.53943428 3 2020-03-09 Monday 3 -0.79224851 4 2020-03-09 Monday 4 0.68168147 5 2020-03-09 Monday 5 -1.02902897 write.csv(file="test1.csv",x1,row.names=FALSE) date1 <- date1 + 1 wday <- weekdays(date1) x2 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) x2 date day lev y 1 2020-03-10 Tuesday 1 -0.6648301 2 2020-03-10 Tuesday 2 -0.1137580 3 2020-03-10 Tuesday 3 1.6532675 4 2020-03-10 Tuesday 4 -0.5845293 5 2020-03-10 Tuesday 5 0.2849392 write.csv(file="test1.csv",x2,row.names=FALSE,append=TRUE) Warning message: In write.csv(file = "test1.csv", x2, row.names = FALSE, append = TRUE) : attempt to set 'append' ignored The values from x2 appear in the test1.csv file. What am I missing, please? Thanks, Erin Erin Hodgess, PhD mailto: erinm.hodgess at gmail.com [[alternative HTML version deleted]]
Hi Erin: The default for write.csv is col.names = TRUE . So, in the second one, if you put, col.names = FALSE, that should work. It's confused right now because you want to append but also write the column names again. Mark On Thu, Jun 4, 2020 at 9:34 PM Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > > Hope everyone is doing as well as possible. > > I have a question about write.csv. I thought that the append argument > would permit adding another data frame with the same column names. > > Here is my example: > > date1 <- as.Date("2020-03-09") > wday <- weekdays(date1) > x1 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > x1 > date day lev y > 1 2020-03-09 Monday 1 -0.09543049 > 2 2020-03-09 Monday 2 0.53943428 > 3 2020-03-09 Monday 3 -0.79224851 > 4 2020-03-09 Monday 4 0.68168147 > 5 2020-03-09 Monday 5 -1.02902897 > write.csv(file="test1.csv",x1,row.names=FALSE) > date1 <- date1 + 1 > wday <- weekdays(date1) > x2 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > x2 > date day lev y > 1 2020-03-10 Tuesday 1 -0.6648301 > 2 2020-03-10 Tuesday 2 -0.1137580 > 3 2020-03-10 Tuesday 3 1.6532675 > 4 2020-03-10 Tuesday 4 -0.5845293 > 5 2020-03-10 Tuesday 5 0.2849392 > write.csv(file="test1.csv",x2,row.names=FALSE,append=TRUE) > Warning message: > In write.csv(file = "test1.csv", x2, row.names = FALSE, append = TRUE) : > attempt to set 'append' ignored > > The values from x2 appear in the test1.csv file. > > What am I missing, please? > > Thanks, > Erin > > > Erin Hodgess, PhD > mailto: erinm.hodgess at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
... : "arguments to write.table: append, col.names, sep, dec and qmethod cannot be altered." The default is FALSE, so cannot be altered. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Jun 4, 2020 at 6:34 PM Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > > Hope everyone is doing as well as possible. > > I have a question about write.csv. I thought that the append argument > would permit adding another data frame with the same column names. > > Here is my example: > > date1 <- as.Date("2020-03-09") > wday <- weekdays(date1) > x1 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > x1 > date day lev y > 1 2020-03-09 Monday 1 -0.09543049 > 2 2020-03-09 Monday 2 0.53943428 > 3 2020-03-09 Monday 3 -0.79224851 > 4 2020-03-09 Monday 4 0.68168147 > 5 2020-03-09 Monday 5 -1.02902897 > write.csv(file="test1.csv",x1,row.names=FALSE) > date1 <- date1 + 1 > wday <- weekdays(date1) > x2 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > x2 > date day lev y > 1 2020-03-10 Tuesday 1 -0.6648301 > 2 2020-03-10 Tuesday 2 -0.1137580 > 3 2020-03-10 Tuesday 3 1.6532675 > 4 2020-03-10 Tuesday 4 -0.5845293 > 5 2020-03-10 Tuesday 5 0.2849392 > write.csv(file="test1.csv",x2,row.names=FALSE,append=TRUE) > Warning message: > In write.csv(file = "test1.csv", x2, row.names = FALSE, append = TRUE) : > attempt to set 'append' ignored > > The values from x2 appear in the test1.csv file. > > What am I missing, please? > > Thanks, > Erin > > > Erin Hodgess, PhD > mailto: erinm.hodgess at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
Use write.table. Write.csv follows a very specific definition for csv... one element of which is that it is not allowed to have a second header line after data lines. The only way for write.csv to enforce this is to disallow appending. On June 4, 2020 6:34:14 PM PDT, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Hello! > >Hope everyone is doing as well as possible. > >I have a question about write.csv. I thought that the append argument >would permit adding another data frame with the same column names. > >Here is my example: > > date1 <- as.Date("2020-03-09") > wday <- weekdays(date1) > x1 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > x1 > date day lev y >1 2020-03-09 Monday 1 -0.09543049 >2 2020-03-09 Monday 2 0.53943428 >3 2020-03-09 Monday 3 -0.79224851 >4 2020-03-09 Monday 4 0.68168147 >5 2020-03-09 Monday 5 -1.02902897 > write.csv(file="test1.csv",x1,row.names=FALSE) > date1 <- date1 + 1 > wday <- weekdays(date1) > x2 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > x2 > date day lev y >1 2020-03-10 Tuesday 1 -0.6648301 >2 2020-03-10 Tuesday 2 -0.1137580 >3 2020-03-10 Tuesday 3 1.6532675 >4 2020-03-10 Tuesday 4 -0.5845293 >5 2020-03-10 Tuesday 5 0.2849392 > write.csv(file="test1.csv",x2,row.names=FALSE,append=TRUE) >Warning message: >In write.csv(file = "test1.csv", x2, row.names = FALSE, append = TRUE) >: > attempt to set 'append' ignored > >The values from x2 appear in the test1.csv file. > >What am I missing, please? > >Thanks, >Erin > > >Erin Hodgess, PhD >mailto: erinm.hodgess at gmail.com > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at 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.-- Sent from my phone. Please excuse my brevity.
Thanks to all for the help. I found that write.table works nicely, with col.names = FALSE with append TRUE. Sincerely, Erin Erin Hodgess, PhD mailto: erinm.hodgess at gmail.com On Thu, Jun 4, 2020 at 7:56 PM Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> Use write.table. > > Write.csv follows a very specific definition for csv... one element of > which is that it is not allowed to have a second header line after data > lines. The only way for write.csv to enforce this is to disallow appending. > > On June 4, 2020 6:34:14 PM PDT, Erin Hodgess <erinm.hodgess at gmail.com> > wrote: > > Hello! > > > >Hope everyone is doing as well as possible. > > > >I have a question about write.csv. I thought that the append argument > >would permit adding another data frame with the same column names. > > > >Here is my example: > > > > date1 <- as.Date("2020-03-09") > > wday <- weekdays(date1) > > x1 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > > x1 > > date day lev y > >1 2020-03-09 Monday 1 -0.09543049 > >2 2020-03-09 Monday 2 0.53943428 > >3 2020-03-09 Monday 3 -0.79224851 > >4 2020-03-09 Monday 4 0.68168147 > >5 2020-03-09 Monday 5 -1.02902897 > > write.csv(file="test1.csv",x1,row.names=FALSE) > > date1 <- date1 + 1 > > wday <- weekdays(date1) > > x2 <- data.frame(date=date1,day=wday,lev=1:5,y=rnorm(5)) > > x2 > > date day lev y > >1 2020-03-10 Tuesday 1 -0.6648301 > >2 2020-03-10 Tuesday 2 -0.1137580 > >3 2020-03-10 Tuesday 3 1.6532675 > >4 2020-03-10 Tuesday 4 -0.5845293 > >5 2020-03-10 Tuesday 5 0.2849392 > > write.csv(file="test1.csv",x2,row.names=FALSE,append=TRUE) > >Warning message: > >In write.csv(file = "test1.csv", x2, row.names = FALSE, append = TRUE) > >: > > attempt to set 'append' ignored > > > >The values from x2 appear in the test1.csv file. > > > >What am I missing, please? > > > >Thanks, > >Erin > > > > > >Erin Hodgess, PhD > >mailto: erinm.hodgess at gmail.com > > > > [[alternative HTML version deleted]] > > > >______________________________________________ > >R-help at 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. > > -- > Sent from my phone. Please excuse my brevity. >[[alternative HTML version deleted]]