Re: [R] write.table
I did not read ?write.table in details about CSV section. Thanks. On 8/10/07, Yinghai Deng <[EMAIL PROTECTED]> wrote: > write.table(mydata.frame, "mydata", col.names=NA, quote=F, sep="\t") will > solve the problem. > Deng > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of Weiwei Shi > Sent: August 10, 2007 12:41 PM > To: r-help@stat.math.ethz.ch > Subject: [R] write.table > > > Hi, > > I am always with this qustion when I tried to write a data.frame with > row.names and col.names. I have to re-make the data frame to let its > first column be the rownames and let row.names=F so that I can align > the colnames correctly. > > Is there a way or option in write.table to automatically do that? > > thanks. > > -- > Weiwei Shi, Ph.D > Research Scientist > GeneGO, Inc. > > "Did you always know?" > "No, I did not. But I believed..." > ---Matrix III > > __ > 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. > > -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III __ 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.
Re: [R] write.table
write.table(mydata.frame, "mydata", col.names=NA, quote=F, sep="\t") will solve the problem. Deng -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Weiwei Shi Sent: August 10, 2007 12:41 PM To: r-help@stat.math.ethz.ch Subject: [R] write.table Hi, I am always with this qustion when I tried to write a data.frame with row.names and col.names. I have to re-make the data frame to let its first column be the rownames and let row.names=F so that I can align the colnames correctly. Is there a way or option in write.table to automatically do that? thanks. -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III __ 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. __ 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.
Re: [R] write.table linebreaks
What do you think the 'eol' argument to write.table is for? I don't have a Mac to hand, but eol='\r' does this on Linux and Windows. On Thu, 19 Jul 2007, Birgit Lemcke wrote: Hello R users, I am a newby using R 2.5.0 on a Apple Power Book G4 with Mac OS X 10.4.10. when I use the write.table function, I always get the output in Unix linebreaks that I have to change to McIntosh linebreaks to be able to Import the data in Excel 2004 for Mac. Is there a possibility to do this automatically in R and respectively in the write.table function? Thanks in advance. Birgit Birgit Lemcke Institut für Systematische Botanik Zollikerstrasse 107 CH-8008 Zürich Switzerland Ph: +41 (0)44 634 8351 [EMAIL PROTECTED] [[alternative HTML version deleted]] -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595__ 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.
Re: [R] write.table: last line should differ from usual eol
Try this: write.ilog <- function(X, file = "") { w <- function(x, z, file) cat("[", paste(x, collapse = ","), "]", z, sep = "", file = file) if (!identical(file, "")) { file <- open(file, "w") on.exit(close(file)) } cat("X=[", file = file) nr <- nrow(X) for(i in 1:nr) w(X[i,], if (i == nr) "];\n" else ",\n", file) invisible(X) } X<-array(1:4,c(2,2)) write.ilog(X) On 6/9/07, jim holtman <[EMAIL PROTECTED]> wrote: > 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]] > > > __ > 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. > > __ 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.
Re: [R] write.table: last line should differ from usual eol
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]] __ 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.
Re: [R] write.table to Excel file
How about using write.csv? Jeff Bricker wrote: > from ?write.table: > > By default there is no column name for a column of row names. If > 'col.names = NA' and 'row.names = TRUE' a blank column name is > added, which is the convention for CSV files to be read by > spreadsheets. > > > On 8/31/06, array chip <[EMAIL PROTECTED]> wrote: > >>Hi, I found that when writing a matrix with row names >>and column names to an Excel file, the Excel file when >>opened has column names shifted towards left resulting >>disalignment. Here is an exmaple >> >>x<-matrix(1:20,nrow=4,dimnames=list(paste('r',1:4,sep=''),paste('c',1:5,sep=''))) >>write.table(x,"xx.xls",sep='\t') >> >>If you open the xx.xls file, you will understand what >>I meant. Is there anyway to solve the disalignment? >> >>Thanks >> >>__ >>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. >> > > > __ > 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. > > -- Kazushige Shimpo<[EMAIL PROTECTED]> Department of Business & Commerce Keio University __ 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.
Re: [R] write.table to Excel file
from ?write.table: By default there is no column name for a column of row names. If 'col.names = NA' and 'row.names = TRUE' a blank column name is added, which is the convention for CSV files to be read by spreadsheets. On 8/31/06, array chip <[EMAIL PROTECTED]> wrote: > Hi, I found that when writing a matrix with row names > and column names to an Excel file, the Excel file when > opened has column names shifted towards left resulting > disalignment. Here is an exmaple > > x<-matrix(1:20,nrow=4,dimnames=list(paste('r',1:4,sep=''),paste('c',1:5,sep=''))) > write.table(x,"xx.xls",sep='\t') > > If you open the xx.xls file, you will understand what > I meant. Is there anyway to solve the disalignment? > > Thanks > > __ > 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. > __ 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.
Re: [R] write.table & csv help
Hi I am not sure about what you want to achieve by sequential writing to a file so maybe you could fill an object in a loop and write it only once. df<-data.frame(matrix(nrow=4,ncol=2)) for(i in seq(1:2)) { df[,i] <- rnorm(4, mean=0, sd=1) } write.table(df,"output.csv", quote = FALSE, sep = ",", row.names = FALSE, col.names = TRUE) HTH Petr From: Sachin J Date: Tue 27 Jun 2006 - 04:34:36 EST Hi, How can I produce the following output in .csv format using write.table function. for(i in seq(1:2)) { df <- rnorm(4, mean=0, sd=1) write.table(df,"C:/output.csv", append = TRUE, quote = FALSE, sep = ",", row.names = FALSE, col.names = TRUE) } Current O/p: x 0.287816 -0.81803 -0.15231 -0.25849 x 2.26831 0.863174 0.269914 0.181486 Desired output x1 x2 0.287816 2.26831 -0.81803 0.863174 -0.15231 0.269914 -0.25849 0.181486 Thanx in advance Sachin -- -- [[alternative HTML version deleted]] -- -- 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 Petr Pikal [EMAIL PROTECTED] __ 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
Re: [R] write.table & csv help
Sachin J wrote: > Hi, > > How can I produce the following output in .csv format using write.table > function. > > for(i in seq(1:2)) > > df <- rnorm(4, mean=0, sd=1) > write.table(df,"C:/output.csv", append = TRUE, quote = FALSE, sep = ",", > row.names = FALSE, col.names = TRUE) > } You cannot append columns with write.table(). Uwe Ligges > Current O/p: > x0.287816-0.81803-0.15231-0.25849x > 2.268310.8631740.2699140.181486 > > Desired output > x1 x20.287816 2.26831-0.81803 0.863174-0.15231 > 0.269914-0.25849 0.181486 > > Thanx in advance > > Sachin > > > - > > [[alternative HTML version deleted]] > > __ > 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 __ 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
Re: [R] write.table() or write.csv() --- add a comment?
On 4/2/2006 9:38 PM, ivo welch wrote: > Dear R group: Is there a way to pass a comment line to write.table() > or write.csv()? presumably, following linux and R conventions, it > would be preceded by a '#' in the output. > > write.csv( object, file="object.csv", comment=paste("this csv file was > created by mycode.R on 12/2/2005"); > > of course, the R read functions are already smart enough to ignore the > comment lines. Open a connection, write the comment to the connection, write the data to the connection, close the connection. e.g. con <- file("object.csv", open="wt") writeLines(paste("# this csv file was created by mycode.R on 12/2/2005"), con) write.csv( object, con) close(con) Duncan Murdoch __ 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
Re: [R] write.table command
Dear Alexander, Thanks for the useful information. ARK - Original Message - From: "Alexander Nervedi" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, March 28, 2006 7:19 PM Subject: RE: [R] write.table command > > Hi! > > Either use write.table() to output a data matrix and read.table() to > retrieve it .. or use save(obj,...) to save R object and then use load to > call it back into an R session. > > check out the help on both > ?read.table > ?save > > hope that helps > > ul-nadi. > > >>From: "Abd Rahman Kassim" <[EMAIL PROTECTED]> >>To: >>Subject: [R] write.table command >>Date: Wed, 29 Mar 2006 08:43:16 -0800 >> >> >>Dear All, >> >> >>I'm trying to save a dataframe using write.table command. It works, but >>when I retrieved, there's an error message as shown below: >> >> > write.table(soil.dat,file="C:/soil.rdata") >> > load("C:/soil.rdata") >>Error: bad restore file magic number (file may be corrupted)-- no data >>loaded >> >> >>I can figure out the error message. Any assistance to solve the problem is >>very much appreciated. >> >>Regards. >> >>Abd. Rahman Kassim, PhD >>Forest Management & Ecology Program >>Forestry & Conservation Division >>Forest Research Institute Malaysia >>Kepong 52109 Selangor >>Malaysia >> >>Fax: 603-62729852 >>Tel: 603-62797179 >> >> >> >>* >> >> >>* >> [[alternative HTML version deleted]] >> >>__ >>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 > > _ > http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/ > > > > * > Outgoing mail is certified Virus Free. > Checked by TrendMicro Interscan Messaging Security. > For any enquiries, please contact FRIM IT Department. > * __ 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
Re: [R] write.table command
Dear Simon, Thanks for the remarks. I had tried read.table and it works. ARK - Original Message - From: "Simon Blomberg" <[EMAIL PROTECTED]> To: "Abd Rahman Kassim" <[EMAIL PROTECTED]>; "R-project help" Sent: Tuesday, March 28, 2006 4:49 PM Subject: Re: [R] write.table command > > The help file for load says that it is to be used for objects saved using > the save command. Perhaps you should try read.table. > > Cheers, > > Simon. > > Abd Rahman Kassim wrote: >> Dear All, >> >> >> I'm trying to save a dataframe using write.table command. It works, but >> when I retrieved, there's an error message as shown below: >> >> >>> write.table(soil.dat,file="C:/soil.rdata") >>> load("C:/soil.rdata") >>> >> Error: bad restore file magic number (file may be corrupted)-- no data >> loaded >> >> >> I can figure out the error message. Any assistance to solve the problem >> is very much appreciated. >> >> Regards. >> >> Abd. Rahman Kassim, PhD >> Forest Management & Ecology Program >> Forestry & Conservation Division >> Forest Research Institute Malaysia >> Kepong 52109 Selangor >> Malaysia >> >> Fax: 603-62729852 >> Tel: 603-62797179 >> >> >> >> * >> >> >> * >> [[alternative HTML version deleted]] >> >> __ >> 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 >> >> > > > -- > Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. > Centre for Resource and Environmental Studies > The Australian National University > Canberra ACT 0200 > Australia > T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au > F: +61 2 6125 0757 > CRICOS Provider # 00120C > > > > * > Outgoing mail is certified Virus Free. > Checked by TrendMicro Interscan Messaging Security. > For any enquiries, please contact FRIM IT Department. > * __ 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
Re: [R] write.table command
The help file for load says that it is to be used for objects saved using the save command. Perhaps you should try read.table. Cheers, Simon. Abd Rahman Kassim wrote: > Dear All, > > > I'm trying to save a dataframe using write.table command. It works, but when > I retrieved, there's an error message as shown below: > > >> write.table(soil.dat,file="C:/soil.rdata") >> load("C:/soil.rdata") >> > Error: bad restore file magic number (file may be corrupted)-- no data loaded > > > I can figure out the error message. Any assistance to solve the problem is > very much appreciated. > > Regards. > > Abd. Rahman Kassim, PhD > Forest Management & Ecology Program > Forestry & Conservation Division > Forest Research Institute Malaysia > Kepong 52109 Selangor > Malaysia > > Fax: 603-62729852 > Tel: 603-62797179 > > > > * > > > * > [[alternative HTML version deleted]] > > __ > 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 > > -- Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. Centre for Resource and Environmental Studies The Australian National University Canberra ACT 0200 Australia T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au F: +61 2 6125 0757 CRICOS Provider # 00120C __ 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
Re: [R] write.table
> Hello! > When using the command "write.table" I want to convert the format: 5e-04 > to .0005. How can I do it? > The only option I found is to use write.matrix but then I cant add rownames. > Thank you > Ronen ?options, see scipen. Or, use format() to convert table before writing it. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] write.table
See the "scipen" argument in ?options. write.table(data.frame(x = 0.0005)) options(scipen = 1) write.table(data.frame(x = 0.0005)) --sundar fluss wrote: > Hello! > When using the command "write.table" I want to convert the format: 5e-04 > to .0005. How can I do it? > The only option I found is to use write.matrix but then I cant add rownames. > Thank you > Ronen > > [[alternative HTML version deleted]] > > __ > 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 __ 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
Re: [R] Write.table: change points to commas when object contains a row of characters
Thank you very much! I wonder why I did not yet come across that function format(). Guess this won ´t be the last time that I use it. The following did exactly what I was looking for: temp<-attributes(jjmat) jjmat<-format(jjmat, decimal.mark=",") attributes(jjmat)<-temp With these changes jjmat was perfect for export to excel. -Ursprüngliche Nachricht- Von: Prof Brian Ripley [mailto:[EMAIL PROTECTED] Gesendet: Mittwoch, 1. Februar 2006 16:31 An: Michael Reinecke Cc: R-help@stat.math.ethz.ch Betreff: Re: [R] Write.table: change points to commas when object contains a row of characters You cannot have a matrix or a data frame which is partially numeric and partially character (within a column for a data frame). You seem rather to have a list matrix. Then ?write.table does say Any columns in a data frame which are lists or have a class (e.g. dates) will be converted by the appropriate 'as.character' method: such columns are unquoted by default. On the other hand, any class information for a matrix is discarded. Although it does not say so, the same happens with a matrix. You need to get a table before you try outputting it: I suggest using format() to help you. On Wed, 1 Feb 2006, Michael Reinecke wrote: > Dear Group! I asked write.table to change the decimal point from "." > to "," , but apparently it would only do so if the object to be > written does not contain any character elements. I would like to > understand, why this has to be so and - of course - find a solution > for my matrix object jjmat, that I tried to write out by > > > > write.table(jjmat, file="jjmat.txt", row.names=TRUE, > col.names=NA,sep="\t",dec=",") > > > > I also tried "options(OutDec=",")" , which changes the presentation on > the console, but seems to have no influence on write table: jjmat is > still written out with points instead of commas. > > > > The object looks like this: > > > >> jjmat > > f2a1 f2b1 f5a1 f5b1 f5c1 > > rowname1 "coltext1" "coltext2" "coltext3" "coltext4" "coltext5" > > rowname2 4,428571 4,326531 4,265306 3,959184 3,306122 > > rowname3 0,469665 0,3328301 0,1776079 -0,1758072 0,0870965 > > rowname4 4,275862 4,206897 4,137931 3,931034 3,379310 > > > >> deparse(jjmat) > > [1] "structure(list(\"coltext1\", 4.42857142857143, 0.469664970752337, " > > > [2] "4.27586206896552, \"coltext2\", 4.3265306122449, > 0.332830055973803, " > > [3] "4.20689655172414, \"coltext3\", 4.26530612244898, > 0.177607859264292, " > > [4] "4.13793103448276, \"coltext4\", 3.95918367346939, > -0.175807245137424, " > > [5] "3.93103448275862, \"coltext5\", 3.30612244897959, > 0.087096493847482, " > > [6] "3.37931034482759), .Dim = c(4, 5), .Dimnames = > list(c(\"rowname1\", " > > [7] "\"rowname2\", \"rowname3\", \"rowname4\"), c(\"f2a1\", \"f2b1\", > \"f5a1\", " > > [8] "\"f5b1\", \"f5c1\")))" > > > > > > Do I have to change the structure of jjmat? Thanks for your comments! > > > > Greetings, > > > > Michael > > > [[alternative HTML version deleted]] > > __ > 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 > -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] Write.table: change points to commas when object contains a row of characters
You cannot have a matrix or a data frame which is partially numeric and partially character (within a column for a data frame). You seem rather to have a list matrix. Then ?write.table does say Any columns in a data frame which are lists or have a class (e.g. dates) will be converted by the appropriate 'as.character' method: such columns are unquoted by default. On the other hand, any class information for a matrix is discarded. Although it does not say so, the same happens with a matrix. You need to get a table before you try outputting it: I suggest using format() to help you. On Wed, 1 Feb 2006, Michael Reinecke wrote: > Dear Group! I asked write.table to change the decimal point from "." to > "," , but apparently it would only do so if the object to be written > does not contain any character elements. I would like to understand, why > this has to be so and - of course - find a solution for my matrix object > jjmat, that I tried to write out by > > > > write.table(jjmat, file="jjmat.txt", row.names=TRUE, > col.names=NA,sep="\t",dec=",") > > > > I also tried "options(OutDec=",")" , which changes the presentation on > the console, but seems to have no influence on write table: jjmat is > still written out with points instead of commas. > > > > The object looks like this: > > > >> jjmat > > f2a1 f2b1 f5a1 f5b1 f5c1 > > rowname1 "coltext1" "coltext2" "coltext3" "coltext4" "coltext5" > > rowname2 4,428571 4,326531 4,265306 3,959184 3,306122 > > rowname3 0,469665 0,3328301 0,1776079 -0,1758072 0,0870965 > > rowname4 4,275862 4,206897 4,137931 3,931034 3,379310 > > > >> deparse(jjmat) > > [1] "structure(list(\"coltext1\", 4.42857142857143, 0.469664970752337, " > > > [2] "4.27586206896552, \"coltext2\", 4.3265306122449, > 0.332830055973803, " > > [3] "4.20689655172414, \"coltext3\", 4.26530612244898, > 0.177607859264292, " > > [4] "4.13793103448276, \"coltext4\", 3.95918367346939, > -0.175807245137424, " > > [5] "3.93103448275862, \"coltext5\", 3.30612244897959, > 0.087096493847482, " > > [6] "3.37931034482759), .Dim = c(4, 5), .Dimnames = > list(c(\"rowname1\", " > > [7] "\"rowname2\", \"rowname3\", \"rowname4\"), c(\"f2a1\", \"f2b1\", > \"f5a1\", " > > [8] "\"f5b1\", \"f5c1\")))" > > > > > > Do I have to change the structure of jjmat? Thanks for your comments! > > > > Greetings, > > > > Michael > > > [[alternative HTML version deleted]] > > __ > 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 > -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] write.table read.table with Dates
In addition to the solutions already provided, note that if *all* you want to do is save your dataframe in a file, and later recreate it from that file, you can use dump(). dump('Tall',file='Tall.r') rm(Tall) ## just to demonstrate that the next command will recreate Tall source('Tall.r') -Don At 2:21 PM +0100 11/10/05, JeeBee wrote: >I've found several similar issues with write.table/read.table >with Dates on this list, but trying to follow this advice I still >get an error. > >First, I read in data from several files, constructing several date/time >columns using ISOdatetime > >> str(Tall$Begin) >'POSIXct', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 >00:00:00" ... >> length(Tall$Begin) >[1] 40114 >> class(Tall$Begin) >[1] "POSIXt" "POSIXct" > >This looks good (time is not always 00:00:00 ...) >This data came from several files, now I want to store the result I have >in data.frame Tall and be able to retrieve this quickly some other time. > >This is what I do: >write.table(Tall, file="somefile.csv", sep=",", qmethod="double", >row.names=FALSE) > >Later, I do this to read the file again: >fieldnames=c("Begin","test-a","test-b","Eind") >T=read.table(file = "somefile.csv", col.names = fieldnames, > header = TRUE, sep = ",", quote="\"", fill=FALSE) > >I understand T$Begin now is a factor. I tried to simply convert it >again using (as I read on this mailinglist ...): >Q = strptime(as.character(T$Begin),format="%Y-%m-%d %H:%M:%S") > >Q is looking good, though its length I don't understand .. is it a list or >something? It seems there are 40114 values in there somewhere... > >> class(Q) >[1] "POSIXt" "POSIXlt" >> length(Q) >[1] 9 >> str(Q) >'POSIXlt', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 >00:00:00" ... > >T$Begin = Q ### yields this error >Error in "$<-.data.frame"(`*tmp*`, "Begin", value = list(sec = c(0, 0, : > replacement has 9 rows, data has 40114 > >Could somebody explain me how to convert the date column? >Or perhaps there is an easier way? > >Thanks in advance for your time. > >__ >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 -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA __ 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
Re: [R] write.table read.table with Dates
On Thu, 10 Nov 2005, JeeBee wrote: > I've found several similar issues with write.table/read.table > with Dates on this list, but trying to follow this advice I still > get an error. > > First, I read in data from several files, constructing several date/time > columns using ISOdatetime > >> str(Tall$Begin) > 'POSIXct', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 > 00:00:00" ... >> length(Tall$Begin) > [1] 40114 >> class(Tall$Begin) > [1] "POSIXt" "POSIXct" > > This looks good (time is not always 00:00:00 ...) > This data came from several files, now I want to store the result I have > in data.frame Tall and be able to retrieve this quickly some other time. > > This is what I do: > write.table(Tall, file="somefile.csv", sep=",", qmethod="double", > row.names=FALSE) > > Later, I do this to read the file again: > fieldnames=c("Begin","test-a","test-b","Eind") > T=read.table(file = "somefile.csv", col.names = fieldnames, > header = TRUE, sep = ",", quote="\"", fill=FALSE) You can avoid all this trouble by using colClasses as documented on the help page. > I understand T$Begin now is a factor. I tried to simply convert it > again using (as I read on this mailinglist ...): > Q = strptime(as.character(T$Begin),format="%Y-%m-%d %H:%M:%S") Or just as.POSIXct(as.character(T$Begin)) > Q is looking good, though its length I don't understand .. is it a list or > something? It seems there are 40114 values in there somewhere... It is a list of length 9. Try names(Q) >> class(Q) > [1] "POSIXt" "POSIXlt" >> length(Q) > [1] 9 >> str(Q) > 'POSIXlt', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 00:00:00" > ... > > T$Begin = Q ### yields this error > Error in "$<-.data.frame"(`*tmp*`, "Begin", value = list(sec = c(0, 0, : >replacement has 9 rows, data has 40114 > > Could somebody explain me how to convert the date column? > Or perhaps there is an easier way? You started with POSIXct, and you need to convert back to POSIXct with as.POSIXct(Q). Reading ?DateTimeClasses should explain to you what you are missing. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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
Re: [R] write.table read.table with Dates
I see that strptime returns a list of year, mon, mday, hour, min, sec, etc. The following works for me (for each column that is a date/time field in my imported file) cat("Converting date/time fields...\n") Q = strptime(as.character(data$myfield), format="%Y-%m-%d%H:%M:%S") data$myfield = ISOdatetime(year = Q$year + 1900, month = Q$mon + 1, day = Q$mday, hour =Q$hour, min = Q$min, sec = Q$sec, tz = "") ISOdatetime does return a vector, which is, I guess, what I want. It is quite slow like this though, and I don't think it's the best way. __ 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
Re: [R] write.table read.table with Dates
On 11/10/05, JeeBee <[EMAIL PROTECTED]> wrote: > I've found several similar issues with write.table/read.table > with Dates on this list, but trying to follow this advice I still > get an error. > > First, I read in data from several files, constructing several date/time > columns using ISOdatetime > > > str(Tall$Begin) > 'POSIXct', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 > 00:00:00" ... > > length(Tall$Begin) > [1] 40114 > > class(Tall$Begin) > [1] "POSIXt" "POSIXct" > > This looks good (time is not always 00:00:00 ...) > This data came from several files, now I want to store the result I have > in data.frame Tall and be able to retrieve this quickly some other time. > > This is what I do: > write.table(Tall, file="somefile.csv", sep=",", qmethod="double", > row.names=FALSE) > > Later, I do this to read the file again: > fieldnames=c("Begin","test-a","test-b","Eind") > T=read.table(file = "somefile.csv", col.names = fieldnames, > header = TRUE, sep = ",", quote="\"", fill=FALSE) > > I understand T$Begin now is a factor. I tried to simply convert it > again using (as I read on this mailinglist ...): > Q = strptime(as.character(T$Begin),format="%Y-%m-%d %H:%M:%S") > > Q is looking good, though its length I don't understand .. is it a list or > something? It seems there are 40114 values in there somewhere... > > > class(Q) > [1] "POSIXt" "POSIXlt" > > length(Q) > [1] 9 > > str(Q) > 'POSIXlt', format: chr [1:40114] "2005-10-02 00:00:00" "2005-10-02 00:00:00" > ... > > T$Begin = Q ### yields this error > Error in "$<-.data.frame"(`*tmp*`, "Begin", value = list(sec = c(0, 0, : >replacement has 9 rows, data has 40114 > > Could somebody explain me how to convert the date column? > Or perhaps there is an easier way? > You are converting it to POSIXlt (which represents date/times as a 9 element structure) but its likely you really wanted to convert it to POSIXct. as.POSIXct(T$Begin) Also, you might need to use the tz= argument depending on what result you want. See the Help Desk article in RNews 4/1 for more info. __ 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
Re: [R] write.table call
RTFM, in particular the "CSV Files" section of ?write.table. BTW, R itself does not write "xls" files. Andy > From: Li,Qinghong,ST.LOUIS,Molecular Biology > > Hi, > > I use write.table() to write a file to an external xls file. > the column names left-shift one position in output file. I > check with col.names() row.names(), the file is fine. How to > prevent the shifting? > > I71 I111I304I307I305I306I114I72 > AFFX-BioB-5_at6.66435 6.7878075.335962 > 5.250163 6.47423 5.8821045.9651096.591687195 > AFFX-BioB-M_at6.1632275.965427 > 4.665569 2.7435316.0972445.77137 > 5.113683 6.314003982 > > Thanks, > Johnny > > [[alternative HTML version deleted]] > > __ > 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 > > __ 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
Re: [R] Write.table query
On On, 2005-03-16, 05:43, Jones, Glen R skrev: > Hello, > > I have the following 'write.table' statement which works fine > > write.table(DataOutput,"c:/Prices.csv",append = TRUE,col.names = NA,sep > = "," ) > > My query is, how could I modify this so I can include a variable name as > a prefix before the 'Prices.CSV' filename. > > For example: > > prefixname = "DevX" > > write.table(DataOutput,"c:/" &prefixname& "Prices.csv",append = > TRUE,col.names = NA,sep = "," ) > > With the resultant CSV output to be named "DevXPrices.csv". Use `paste': > prefixname <- "DevX" > paste("c:/", prefixname, "Prices.csv", sep = "") [1] "c:/DevXPrices.csv" HTH, Henric __ 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
Re: [R] write.table(NULL)
On Tue, 3 Aug 2004, Thomas Lumley wrote: > On Tue, 3 Aug 2004, Jack Tanner wrote: > > > > write.table(NULL) > > Error in which(unlist(lapply(x, function(x) is.character(x) || > > is.factor(x : argument to "which" is not logical > > > > Is this correct behavior? It seems harsh to abort an entire run just > > because one of the tables you generated happened to be NULL. > > > > Well, yes, in a perfect world write.table(NULL) would just write no > output. It's arguably even a bug, or at least the fact that the same > thing happens with a zero-length data frame is arguably a bug. I'll fix > it. It's zero-column cases it gets in trouble with: zero-row cases are handled correctly AFAICS. Depends what `zero-length' means. We should change it, although what is the right output is less clear to me. You can have zero-columns and non-zero rows and with row names. Compare > hills[FALSE] NULL data frame with 35 rows with > as.matrix(hills[FALSE]) (a column of rownames). -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] write.table(NULL)
On Tue, 3 Aug 2004, Jack Tanner wrote: > > write.table(NULL) > Error in which(unlist(lapply(x, function(x) is.character(x) || > is.factor(x : argument to "which" is not logical > > Is this correct behavior? It seems harsh to abort an entire run just > because one of the tables you generated happened to be NULL. > Well, yes, in a perfect world write.table(NULL) would just write no output. It's arguably even a bug, or at least the fact that the same thing happens with a zero-length data frame is arguably a bug. I'll fix it. There are quite a lot of functions that treat you harshly when you supply zero-length input. This is usually for a similar reason to the one here: when lapply returns no output the information as to whether it is no logical values or no vectors or no whatever else gets lost. It's a good idea to be cautious about zero-length variables as a programmer. -thomas __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] write.table when keeping column headers (names of Columns in matrix) and row numbers
>From the details of ?write.table (and in the Data Import/Export manual and in MASS4 ...) Normally there is no column name for a column of row names. If 'col.names=NA' a blank column name is added. This can be used to write CSV files for input to spreadsheets. What can we possible do to make this more obvious? On Tue, 22 Jun 2004, Peter Wilkinson wrote: > When using the write.table (say for a tab delimited file) command on a > matrix with Row and Columns, the column headers are always being left > shifted into the column where the row numbers are being placed. One can see > this when you open up the tab delimited file in excel. > > Is there a better command for this, or is this supposed to be a 'feature'. It is a genuinely useful feature, fully documented. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] write.table file="file.txt" help
Kristin Kay Nicodemus wrote: Hi all, I have a R script that creates several input files for an analysis program. It loops through the matrix read into R and picks out submatrices and then creates a separate output file for each submatrix. The loop works great, but I am having trouble getting all the separate output files written. The line I have is: write.table(ch1d, file="C:/WINDOWS/Desktop/SNPs/haplo.txt", row.names=F, col.names=F, append=F, quote=F) Which works just fine if I just wanted to create a single file from the loop. However, I need to somehow get it to change the name of the output file ("haplo.txt") each time it goes through the loop so it doesn't overwrite each time. In perl, I'd create $n=1 and increment up each loop, and call the file something like "haplo.txt.$n" I tried to do something like that but R doesn't recognize the variable that would be $n in perl (because it's part of the quoted name of the output file). Adding it after the ending " just gave me an error, as I thought it would. I also tried to use system(copy ...) to change the name of the file in dos, but my knowledge of dos is abysmal, so I was unable to do it. Any ideas on how to go about doing this would be most appreciated! Thanks in advance, KK Nicodemus Use paste(). for(i in 1:n) { file <- paste("C:/WINDOWS/Desktop/SNPs/haplo", i, "txt", sep = ".") cat("Writing data to", file, "\n") write.table(ch1d, file=file, row.names=F, col.names=F, append=F, quote=F) } -sundar __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] write.table file="file.txt" help
Thanks to Andy Bunn and Patrick Connolly for their help! Kristin Nicodemus __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] write.table file="file.txt" help
Look at ?paste for (j in 1:10) { write.table(j, file=paste("haplo.txt", j, sep="."), row.names=F, col.names=F, append=F, quote=F) } BTW, there have been many similar posts like this in the past. They are easily found using the search function at http://cran.r-project.org/search.html HTH, Andy __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] write.table() with "numeric" and "complex" data
On Thu, 16 Jan 2003, Cyril Humbert wrote: > write.table() seems to write "numeric" data as "complex" > if one column of the data.frame is "complex". Is it a > way to avoid this ? Comment the line x <- as.matrix(x) in write.table. > > For example: > > > df <- data.frame(x=c(1,2), z=c(1i+1, 2i+2)) > > df > xz > 1 1 1+1i > 2 2 2+2i > > > mode(df$x) > [1] "numeric" > > > write.table(df, file="aa.dat") > > df<-read.table("aa.dat", header=TRUE) > > df >xz > 1 1+0i 1+1i > 2 2+0i 2+2i > > > mode(df$x) > [1] "complex" > > > version > platform i386-pc-linux-gnu > major1 > minor6.2 > > > Thanks > > -- > Cyril > > __ > [EMAIL PROTECTED] mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help