[R] Formatting summary() output to a file

Rich Shepard r@hep@rd @ending from @ppl-eco@y@@com
Fri Jul 27 17:20:16 CEST 2018


   I want to save the output of summary(df_name) to a disk file and my
research found that the sink() function is what I want to use. The 'R Cookbook'
provides a an alternative example using cat() to a connection. Here,

con <- file("wysumallyrs.txt", "w")
cat(summary(wyallyrs), file=con)
close(con)

produces this output:

Length:402531      Class :character   Mode  :character   NA NA NA NA
Length:402531      Class :character   Mode  :character   NA NA NA NA Min.
:90.65   1st Qu.:93.81   Median :94.14   Mean   :93.86   3rd Qu.:94.43
Max. :98.91   NA's   :225   Min. :1988-10-01   1st Qu.:1996-02-01   Median
:2001-12-01   Mean   :2002-07-28   3rd Qu.:2008-09-10   Max. :2018-06-21
NA Min. :1988-10-01 00:30:00   1st Qu.:1996-02-01 00:45:00   Median
:2001-12-01 15:30:00   Mean   :2002-07-29 03:04:28   3rd Qu.:2008-09-10
16:00:00   Max. :2018-06-21 00:00:00   NA

   Is there a way to format this output as it is on the console when the
script contains

sum <- summary(wyallyrs)
print(sum)

      date               time                elev           myDate
  Length:402531      Length:402531      Min.   :90.65   Min.   :1988-10-01
  Class :character   Class :character   1st Qu.:93.81   1st Qu.:1996-02-01
  Mode  :character   Mode  :character   Median :94.14   Median :2001-12-01
                                        Mean   :93.86   Mean   :2002-07-28
                                        3rd Qu.:94.43   3rd Qu.:2008-09-10
                                        Max.   :98.91   Max.   :2018-06-21
                                        NA's   :225
      myTime
  Min.   :1988-10-01 00:30:00
  1st Qu.:1996-02-01 00:45:00
  Median :2001-12-01 15:30:00
  Mean   :2002-07-29 03:04:28
  3rd Qu.:2008-09-10 16:00:00
  Max.   :2018-06-21 00:00:00

TIA,

Rich



More information about the R-help mailing list