56

When working with data (e.g., in data.frame) the user can control displaying digits by using

options(digits=3) 

and listing the data.frame like this.

ttf.all

When the user needs to paste the data in Excell like this

write.table(ttf.all, 'clipboard', sep='\t',row.names=F)

The digits parameter is ignored and numbers are not rounded.

See nice output

> ttf.all
  year V1.x.x V1.y.x ratio1 V1.x.y V1.y.y ratioR V1.x.x V1.y.x ratioAL V1.x.y V1.y.y ratioRL
1 2006    227    645   35.2     67    645   10.4    150    645    23.3     53    645    8.22
2 2007    639   1645   38.8    292   1645   17.8    384   1645    23.3    137   1645    8.33
3 2008   1531   3150   48.6    982   3150   31.2    755   3150    24.0    235   3150    7.46
4 2009   1625   3467   46.9   1026   3467   29.6    779   3467    22.5    222   3467    6.40

But what is in excel (clipboard) is not rounded. How to control in in write.table()?

smci
  • 26,085
  • 16
  • 96
  • 138
userJT
  • 9,328
  • 17
  • 65
  • 82
  • 12
    Use `round()`... – Andrie Jan 10 '13 at 14:54
  • 2
    if I understand right, round() would have to be applied on a set of columns and I would have to enumerate those. The digits=3 is global and does not require column enumeration. so round() is not a perfect solution. – userJT Jan 11 '13 at 15:09

2 Answers2

60

You can use the function format() as in:

write.table(format(ttf.all, digits=2), 'clipboard', sep='\t',row.names=F)

format() is a generic function that has methods for many classes, including data.frames. Unlike round(), it won't throw an error if your dataframe is not all numeric. For more details on the formatting options, see the help file via ?format

MattBagg
  • 9,278
  • 3
  • 35
  • 47
  • 3
    When I use both `digits=2` and `scientific=F`, the digits setting is ignored. It works fine if I allow scientific. Any ideas? – jzadra Jun 22 '15 at 15:48
  • Dear @jzadra in my case, haveing a dataframe of long scientific numbers mixed with smaller numbers, I used these options in order to properly see in R numbers, as well to properly save data in csv file, as I see them in R: `general R option : options(scipen=1, digits=8), and for write.table(format(data_table, digits=4, scientific=F)` – Elias EstatisticsEU May 24 '17 at 07:32
  • In some way, by setting in general R environment the number of decimal digits to be displayed, it is passed into write table command, resulting numbers without decimal numbers! Therefore, you must increase decimal digits in write.table relatively. – Elias EstatisticsEU May 24 '17 at 07:38
  • 1
    `character` conversion can be quite slow, so this is not ideal – MichaelChirico Dec 08 '17 at 05:50
  • Using `write.table(format(1.019, digits=2), file= paste0(getwd(), "/test.csv"))`, for example, gives me a .csv with the value `1`, algouth I would expect `1.02` – machine Jul 24 '19 at 10:01
4

Adding a solution for data frame having mixed character and numeric columns. We first use mutate_if to select numeric columns then apply the round() function to them.

# install.packages('dplyr', dependencies = TRUE)
library(dplyr)

df <- read.table(text = "id  year V1.x.x V1.y.x ratio1
a 2006    227.11111    645.11111   35.22222  
b 2007    639.11111   1645.11111   38.22222  
c 2008   1531.11111   3150.11111   48.22222  
d 2009   1625.11111   3467.11111   46.22222",
                 header = TRUE, stringsAsFactors = FALSE)

df %>% 
  mutate_if(is.numeric, round, digits = 2)
#>   id year  V1.x.x  V1.y.x ratio1
#> 1  a 2006  227.11  645.11  35.22
#> 2  b 2007  639.11 1645.11  38.22
#> 3  c 2008 1531.11 3150.11  48.22
#> 4  d 2009 1625.11 3467.11  46.22

### dplyr v1.0.0+
df %>% 
  mutate(across(where(is.numeric), ~ round(., digits = 2)))
#>   id year  V1.x.x  V1.y.x ratio1
#> 1  a 2006  227.11  645.11  35.22
#> 2  b 2007  639.11 1645.11  38.22
#> 3  c 2008 1531.11 3150.11  48.22
#> 4  d 2009 1625.11 3467.11  46.22

Created on 2019-03-17 by the reprex package (v0.2.1.9000)

Tung
  • 20,273
  • 6
  • 66
  • 83