Andreas Gegg
2007-Jun-09 10:31 UTC
[R] write.table: last line should differ from usual eol
Dear R-Team,
I have a problem with writing an array to (for example) a .txt-file.
Because of the .txt-file must be read from another programm (OPL ILOG),
the syntax of the output must be from a special form:
name_of_the_object = [ [1,2, ... ],
[1,...],
... ];
I think it's easier to understand with a small example:
X<-array(1:4,c(2,2))
should be written as:
X = [[1,3],
[2,4]];
I have (until now) used the following:
write("X=[[",file=filename)
write.table(X,file=filename,sep=",",eol="],\n [",
row.names=FALSE,
col.names=FALSE,append=TRUE)
which leads to the following output:
X=[[
1,3],
[2,4],
[
I hope you can help because it's very annoying to adjust the resulting
.txt-file "by hand".
Thanks a lot for your help!
With nice greetings
Andreas Gegg,
mathematic-student on Catholic University of Eichst?tt-Ingolstadt (Germany)
This will probably do it for you. It is a function to create the output:
write.array <- function(x,fileName){
outFile <- file(fileName, 'w')
cat(deparse(substitute(x)), "=[", sep='', file=outFile)
for (i in 1:nrow(x)){
cat('[', paste(x[i,], collapse=','), ']',
file=outFile, sep='')
if (i == nrow(x)) cat('];', file=outFile, sep='')
else cat(',\n', file=outFile, sep='')
}
close(outFile)
}
# test data
a <- matrix(1:25,5)
write.array(a, '/tempxx.txt')
Here is the output file:
a=[[1,6,11,16,21],
[2,7,12,17,22],
[3,8,13,18,23],
[4,9,14,19,24],
[5,10,15,20,25]];
I have a problem with writing an array to (for example) a .txt-file.
Because of the .txt-file must be read from another programm (OPL ILOG),
the syntax of the output must be from a special form:
name_of_the_object = [ [1,2, ... ],
[1,...],
... ];
I think it's easier to understand with a small example:
X<-array(1:4,c(2,2))
should be written as:
X = [[1,3],
[2,4]];
I have (until now) used the following:
write("X=[[",file=filename)
write.table(X,file=filename,sep=",",eol="],\n [",
row.names=FALSE,
col.names=FALSE,append=TRUE)
which leads to the following output:
X=[[
1,3],
[2,4],
[
I hope you can help because it's very annoying to adjust the resulting
.txt-file "by hand".
Thanks a lot for your help!
With nice greetings
Andreas Gegg,
mathematic-student on Catholic University of Eichstätt-Ingolstadt (Germany)
______________________________________________
R-help@stat.math.ethz.ch 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.
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
[[alternative HTML version deleted]]
Seemingly Similar Threads
- Samba freezes
- Feature request: extend functionality of 'unlist()' by args 'delim=c("/", "_", etc.)' and 'keep.special=TRUE/FALSE'
- Problems building own package (Error: "package has been build before R-2.10.0")
- Problems building own package (Error: "package has been build before R-2.10.0")
- Samba freezes