An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110125/5aa69ca0/attachment.pl>
write.table -- maintain decimal places
8 messages · Sébastien Bihorel, Peter Ehlers, Jim Moon
On Tue, 25 Jan 2011 16:16:37 -0800,
Jim Moon <moonja at ohsu.edu> wrote:
Hello, All, How can I maintain the decimal places when using write.table()?
Have a look at ?format.data.frame
Seb
On 2011-01-25 16:16, Jim Moon wrote:
Hello, All, How can I maintain the decimal places when using write.table()? Jim e.g. df: EFFECT2 PVALUE 1 0.023 0.88080 2 -0.260 0.08641 3 -0.114 0.45200 write.table(df,file='df.txt',quote=F,sep='\t',row.names=F)
write.table(format(df, drop0trailing=FALSE), ....) Peter Ehlers
df.txt: EFFECT2 PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
Thank you for the response, Peter. The approach: write.table(format(df, drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) surprisingly still results in some loss of trailing 0's. df: EFFECT2 PVALUE 1 0.023 0.88080 2 -0.260 0.08641 3 -0.114 0.45200 df.txt: EFFECT2 PVALUE 0.023 8.808e-01 -0.26 8.641e-02 -0.114 4.520e-01 -----Original Message----- From: Peter Ehlers [mailto:ehlers at ucalgary.ca] Sent: Tuesday, January 25, 2011 5:09 PM To: Jim Moon Cc: r-help at r-project.org Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 16:16, Jim Moon wrote:
Hello, All, How can I maintain the decimal places when using write.table()? Jim e.g. df: EFFECT2 PVALUE 1 0.023 0.88080 2 -0.260 0.08641 3 -0.114 0.45200 write.table(df,file='df.txt',quote=F,sep='\t',row.names=F)
write.table(format(df, drop0trailing=FALSE), ....) Peter Ehlers
df.txt: EFFECT2 PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
On 2011-01-25 17:22, Jim Moon wrote:
Thank you for the response, Peter. The approach: write.table(format(df, drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) surprisingly still results in some loss of trailing 0's.
What version of R? I'm using R version 2.12.1 Patched (2010-12-27 r53883) and it works for me. Peter Ehlers
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
df.txt:
EFFECT2 PVALUE
0.023 8.808e-01
-0.26 8.641e-02
-0.114 4.520e-01
-----Original Message-----
From: Peter Ehlers [mailto:ehlers at ucalgary.ca]
Sent: Tuesday, January 25, 2011 5:09 PM
To: Jim Moon
Cc: r-help at r-project.org
Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 16:16, Jim Moon wrote:
Hello, All,
How can I maintain the decimal places when using write.table()?
Jim
e.g.
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
write.table(df,file='df.txt',quote=F,sep='\t',row.names=F)
write.table(format(df, drop0trailing=FALSE), ....) Peter Ehlers
df.txt: EFFECT2 PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
On 2011-01-25 17:22, Jim Moon wrote:
Thank you for the response, Peter. The approach: write.table(format(df, drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) surprisingly still results in some loss of trailing 0's.
Here are a couple more (essentially identical) ways:
# 1.
dfm <- within(df, {
EFFECT2 <- sprintf("%6.3f", EFFECT2)
PVALUE <- sprintf("%7.5f", PVALUE)
})
# 2.
dfm <- within(df, {
EFFECT2 <- formatC(EFFECT2, format="f", digits=3)
PVALUE <- formatC(PVALUE, format="f", digits=5)
})
write.table(dfm, file='dfm.txt', quote=FALSE, sep='\t', row.names=FALSE)
Peter Ehlers
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
df.txt:
EFFECT2 PVALUE
0.023 8.808e-01
-0.26 8.641e-02
-0.114 4.520e-01
-----Original Message-----
From: Peter Ehlers [mailto:ehlers at ucalgary.ca]
Sent: Tuesday, January 25, 2011 5:09 PM
To: Jim Moon
Cc: r-help at r-project.org
Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 16:16, Jim Moon wrote:
Hello, All,
How can I maintain the decimal places when using write.table()?
Jim
e.g.
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
write.table(df,file='df.txt',quote=F,sep='\t',row.names=F)
write.table(format(df, drop0trailing=FALSE), ....) Peter Ehlers
df.txt: EFFECT2 PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
I am using: "R version 2.11.1 (2010-05-31)" It is good to know that it works in 2.12.1 Jim -----Original Message----- From: Peter Ehlers [mailto:ehlers at ucalgary.ca] Sent: Tuesday, January 25, 2011 5:57 PM To: Jim Moon Cc: r-help at r-project.org Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 17:22, Jim Moon wrote:
Thank you for the response, Peter. The approach: write.table(format(df, drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) surprisingly still results in some loss of trailing 0's.
What version of R? I'm using R version 2.12.1 Patched (2010-12-27 r53883) and it works for me. Peter Ehlers
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
df.txt:
EFFECT2 PVALUE
0.023 8.808e-01
-0.26 8.641e-02
-0.114 4.520e-01
-----Original Message-----
From: Peter Ehlers [mailto:ehlers at ucalgary.ca]
Sent: Tuesday, January 25, 2011 5:09 PM
To: Jim Moon
Cc: r-help at r-project.org
Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 16:16, Jim Moon wrote:
Hello, All,
How can I maintain the decimal places when using write.table()?
Jim
e.g.
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
write.table(df,file='df.txt',quote=F,sep='\t',row.names=F)
write.table(format(df, drop0trailing=FALSE), ....) Peter Ehlers
df.txt: EFFECT2 PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
Great. Thank you, Peter! -----Original Message----- From: Peter Ehlers [mailto:ehlers at ucalgary.ca] Sent: Tuesday, January 25, 2011 7:26 PM To: Jim Moon Cc: r-help at r-project.org Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 17:22, Jim Moon wrote:
Thank you for the response, Peter. The approach: write.table(format(df, drop0trailing=FALSE),file='df.txt',quote=F,sep='\t',row.names=F) surprisingly still results in some loss of trailing 0's.
Here are a couple more (essentially identical) ways:
# 1.
dfm <- within(df, {
EFFECT2 <- sprintf("%6.3f", EFFECT2)
PVALUE <- sprintf("%7.5f", PVALUE)
})
# 2.
dfm <- within(df, {
EFFECT2 <- formatC(EFFECT2, format="f", digits=3)
PVALUE <- formatC(PVALUE, format="f", digits=5)
})
write.table(dfm, file='dfm.txt', quote=FALSE, sep='\t', row.names=FALSE)
Peter Ehlers
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
df.txt:
EFFECT2 PVALUE
0.023 8.808e-01
-0.26 8.641e-02
-0.114 4.520e-01
-----Original Message-----
From: Peter Ehlers [mailto:ehlers at ucalgary.ca]
Sent: Tuesday, January 25, 2011 5:09 PM
To: Jim Moon
Cc: r-help at r-project.org
Subject: Re: [R] write.table -- maintain decimal places
On 2011-01-25 16:16, Jim Moon wrote:
Hello, All,
How can I maintain the decimal places when using write.table()?
Jim
e.g.
df:
EFFECT2 PVALUE
1 0.023 0.88080
2 -0.260 0.08641
3 -0.114 0.45200
write.table(df,file='df.txt',quote=F,sep='\t',row.names=F)
write.table(format(df, drop0trailing=FALSE), ....) Peter Ehlers
df.txt: EFFECT2 PVALUE 0.023 0.8808 -0.26 0.08641 -0.114 0.452 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.