[R] Editing data sheet issue with write.table append

David Winsemius dwinsemius at comcast.net
Sat Apr 20 19:16:34 CEST 2013


On Apr 20, 2013, at 1:29 AM, Vinny Moriarty wrote:

> I'm running R 2.15.2 on Max OS X 10.6.8
> 
> 
> If I write a simple file
> 
> Data1<-1
> DF<-data.frame(Data1)
> colnames(DF)<-"Col1"
> 
> and write to a csv file
> 
> write.table(DF,file="Data.csv",sep=",",row.names=FALSE,col.names=TRUE)
> 
> I can then append to it no problem
> 
> Data2<-2
> Data3<-4
> 
> 
> DF2<-data.frame(Data2)
> DF3<-data.frame(Data3)
> 
> write.table(DF2,file="Data.csv",sep=",",row.names=FALSE,col.names=FALSE,append=TRUE)
> write.table(DF3,file="Data.csv",sep=",",row.names=FALSE,col.names=FALSE,append=TRUE)
> 
> 
> All good so far.
> 
> But the problem arises if I then need to edit the data sheet and continue
> to append using the above code. If I open up the data sheet in excel and
> delete a line, the next time I try and append the data as above the first
> new line of data starts at the end of the last row of data (though all
> subsequent new lines are appended properly).
> 
> I'm sure when I manually delete the line in excel I am removing the
> invisible carriage return character, causing the first line of new data to
> be added onto where the carriage return used to be.
> 
> Is there a way to specify in append=TRUE to always start with a new line of
> data in the spreadsheet?

No. 

> Failing that, is there a work around to avoid this
> problem?

You could always execute this before each append effort to restore the Excel-induced missing end of line character:

cat"\n", file="Data.csv", append=TRUE)


-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list