[R] transform summary table list to summary table (for .csv output)

arun smartpink111 at yahoo.com
Sat Jun 15 19:15:22 CEST 2013


Hi,
You could try this:
#In one of the list element

SampleSummary$ConcLow
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
# 0.4200  0.6100  0.7300  0.7373  0.8150  1.6600       1 

#additional entry for NA.  You may need to check the data and remove the missing value.  Here, I removed that entry.


library(stringr)

res<-data.frame(lapply(SampleSummary,function(x) {x1<-x[names(x)!="NA's"];Nch1<-max(nchar(names(x1)));x2<-str_pad(names(x1),Nch1,side="right",pad=" ");paste(x2,x1,sep=":") }),stringsAsFactors=FALSE)
 res1<-as.table(as.matrix(res))

dimnames(res1)[[1]]<-rep("",nrow(res1))
 str(res1)
# 'table' chr [1:6, 1:17] "Min.   :1995-10-10" "1st Qu.:1996-06-16" ...
# - attr(*, "dimnames")=List of 2
#  ..$ : chr [1:6] "" "" "" "" ...
#  ..$ : chr [1:17] "Date" "ConcLow" "ConcHigh" "Uncen" ...

 str(summarySample)
# 'table' chr [1:6, 1:17] "Min.   :2002-05-23  " "1st Qu.:2002-09-02  " ...
# - attr(*, "dimnames")=List of 2
#  ..$ : chr [1:6] "" "" "" "" ...
#  ..$ : chr [1:17] "     Date" "   ConcLow" "   ConcHigh" "    Uncen" ...
 summarySample[,1:3]
#      Date                 ConcLow            ConcHigh       
# "Min.   :2002-05-23  " "Min.   :0.5800  " "Min.   :0.5800  "
# "1st Qu.:2002-09-02  " "1st Qu.:0.7525  " "1st Qu.:0.7525  "
# "Median :2003-01-07  " "Median :0.7800  " "Median :0.7800  "
# "Mean   :2003-01-16  " "Mean   :0.8100  " "Mean   :0.8100  "
# "3rd Qu.:2003-05-29  " "3rd Qu.:0.9025  " "3rd Qu.:0.9025  "
# "Max.   :2003-09-04  " "Max.   :1.0900  " "Max.   :1.0900  "
 res1[,1:3]
# Date                 ConcLow          ConcHigh        
# "Min.   :1995-10-10" "Min.   :0.42"   "Min.   :0.27"  
# "1st Qu.:1996-06-16" "1st Qu.:0.61"   "1st Qu.:0.605" 
# "Median :1996-12-10" "Median :0.73"   "Median :0.7275"
# "Mean   :1998-01-08" "Mean   :0.7373" "Mean   :0.7322"
# "3rd Qu.:1998-03-12" "3rd Qu.:0.815"  "3rd Qu.:0.8125"
# "Max.   :2003-09-04" "Max.   :1.66"   "Max.   :1.66"  


#There is some space on the right for each column of summarySample.   Not sure if you wanted that in res1. 

A.K.


Hi, I have a R object called Sample which I am obtaining the summary 
table for through 2 methods: summarySample <- summary(Sample) & 
SampleSummary <- lapply(Sample, summary). I would like to transform 
the results of lapply(Sample, summary) into the same format as 
summary(Sample). I have included dput for both summary results. After I 
obtain the summary results I am exporting the table to a .csv and I 
would like both .csv files to be similarly formatted; however, I am not 
able to do that now. I have searched online for assistance, but I have 
not been successful yet. 

Thank you. 

Irucka Embry 

summarySample 
dput(summarySample) 
structure(c("Min.   :2002-05-23  ", "1st Qu.:2002-09-02  ", "Median :2003-01-07  ", 
"Mean   :2003-01-16  ", "3rd Qu.:2003-05-29  ", "Max.   :2003-09-04  ", 
"Min.   :0.5800  ", "1st Qu.:0.7525  ", "Median :0.7800  ", "Mean   :0.8100  ", 
"3rd Qu.:0.9025  ", "Max.   :1.0900  ", "Min.   :0.5800  ", "1st Qu.:0.7525  ", 
"Median :0.7800  ", "Mean   :0.8100  ", "3rd Qu.:0.9025  ", "Max.   :1.0900  ", 
"Min.   :1  ", "1st Qu.:1  ", "Median :1  ", "Mean   :1  ", "3rd Qu.:1  ", 
"Max.   :1  ", "Min.   :0.5800  ", "1st Qu.:0.7525  ", "Median :0.7800  ", 
"Mean   :0.8100  ", "3rd Qu.:0.9025  ", "Max.   :1.0900  ", "Min.   :55659  ", 
"1st Qu.:55762  ", "Median :55888  ", "Mean   :55898  ", "3rd Qu.:56031  ", 
"Max.   :56128  ", "Min.   : 2  ", "1st Qu.: 5  ", "Median : 7  ", 
"Mean   : 7  ", "3rd Qu.: 9  ", "Max.   :12  ", "Min.   : 35.0  ", 
"1st Qu.:142.5  ", "Median :201.5  ", "Mean   :199.3  ", "3rd Qu.:247.2  ", 
"Max.   :345.0  ", "Min.   :2002  ", "1st Qu.:2003  ", "Median :2003  ", 
"Mean   :2003  ", "3rd Qu.:2003  ", "Max.   :2004  ", "Min.   :1829  ", 
"1st Qu.:1833  ", "Median :1837  ", "Mean   :1837  ", "3rd Qu.:1841  ", 
"Max.   :1845  ", "Min.   :-0.9670  ", "1st Qu.:-0.8075  ", "Median :-0.2531  ", 
"Mean   :-0.1224  ", "3rd Qu.: 0.5788  ", "Max.   : 0.9906  ", 
"Min.   :-0.9917  ", "1st Qu.:-0.7986  ", "Median :-0.5209  ", 
"Mean   :-0.3123  ", "3rd Qu.: 0.1663  ", "Max.   : 0.9327  ", 
"Min.   :0.1189  ", "1st Qu.:0.5154  ", "Median :1.3734  ", "Mean   :1.6672  ", 
"3rd Qu.:2.6618  ", "Max.   :5.7483  ", "Min.   :-2.12921  ", 
"1st Qu.:-0.71031  ", "Median : 0.30516  ", "Mean   : 0.02173  ", 
"3rd Qu.: 0.97834  ", "Max.   : 1.74891  ", "Min.   :-0.4057  ", 
"1st Qu.:-0.2986  ", "Median :-0.2131  ", "Mean   :-0.2022  ", 
"3rd Qu.:-0.1459  ", "Max.   : 0.0798  ", "Min.   :0.02030  ", 
"1st Qu.:0.03857  ", "Median :0.06197  ", "Mean   :0.06628  ", 
"3rd Qu.:0.08225  ", "Max.   :0.17336  ", "Min.   :0.6766  ", 
"1st Qu.:0.7432  ", "Median :0.8117  ", "Mean   :0.8259  ", "3rd Qu.:0.8646  ", 
"Max.   :1.0838  "), .Dim = c(6L, 17L), .Dimnames = list(c("", 
"", "", "", "", ""), c("     Date", "   ConcLow", "   ConcHigh", 
"    Uncen", "   ConcAve", "    Julian", "    Month", "     Day", 
"   DecYear", "   MonthSeq", "    SinDY", "    CosDY", "      Q", 
"     LogQ", "     yHat", "      SE", "   ConcHat")), class = "table") 


SampleSummary 
dput(SampleSummary) 
structure(list(Date = structure(c(9413, 9663, 9840, 10234.0652174, 
10297.75, 12299), .Names = c("Min.", "1st Qu.", "Median", "Mean", 
"3rd Qu.", "Max."), class = c("summaryDefault", "table", "Date" 
)), ConcLow = structure(c(0.42, 0.61, 0.73, 0.7373, 0.815, 1.66, 
1), .Names = c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", 
"Max.", "NA's"), class = c("summaryDefault", "table")), ConcHigh = structure(c(0.27, 
0.605, 0.7275, 0.7322, 0.8125, 1.66), .Names = c("Min.", "1st Qu.", 
"Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), Uncen = structure(c(0, 1, 1, 0.9891, 1, 1), .Names = c("Min.", 
"1st Qu.", "Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), ConcAve = structure(c(0.135, 0.605, 0.7275, 0.7307, 
0.8125, 1.66), .Names = c("Min.", "1st Qu.", "Median", "Mean", 
"3rd Qu.", "Max."), class = c("summaryDefault", "table")), Julian = structure(c(53240, 
53490, 53670, 54060, 54130, 56130), .Names = c("Min.", "1st Qu.", 
"Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), Month = structure(c(1, 3.75, 6, 6.315, 9, 12), .Names = c("Min.", 
"1st Qu.", "Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), Day = structure(c(6, 89.5, 173.5, 175, 249.5, 356), .Names = c("Min.", 
"1st Qu.", "Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), DecYear = structure(c(1996, 1996, 1997, 1998, 1998, 
2004), .Names = c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", 
"Max."), class = c("summaryDefault", "table")), MonthSeq = structure(c(1750, 
1758, 1764, 1777, 1779, 1845), .Names = c("Min.", "1st Qu.", 
"Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), SinDY = structure(c(-0.9999, -0.7539, 0.1113, 0.02759, 
0.6849, 1), .Names = c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", 
"Max."), class = c("summaryDefault", "table")), CosDY = structure(c(-1, 
-0.7595, -0.1707, -0.08807, 0.5758, 0.9955), .Names = c("Min.", 
"1st Qu.", "Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), Q = structure(c(0.1189, 0.7646, 1.529, 2.951, 2.895, 
24.41), .Names = c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", 
"Max."), class = c("summaryDefault", "table")), LogQ = structure(c(-2.129, 
-0.2685, 0.4245, 0.492, 1.063, 3.195), .Names = c("Min.", "1st Qu.", 
"Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), yHat = structure(c(-0.5147, -0.4256, -0.3506, -0.3377, 
-0.2826, 0.1143), .Names = c("Min.", "1st Qu.", "Median", "Mean", 
"3rd Qu.", "Max."), class = c("summaryDefault", "table")), SE = structure(c(0.1045, 
0.1934, 0.2169, 0.2214, 0.2622, 0.3152), .Names = c("Min.", "1st Qu.", 
"Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table")), ConcHat = structure(c(0.601, 0.6781, 0.7206, 0.736, 
0.7777, 1.127), .Names = c("Min.", "1st Qu.", "Median", "Mean", 
"3rd Qu.", "Max."), class = c("summaryDefault", "table"))), .Names = c("Date", 
"ConcLow", "ConcHigh", "Uncen", "ConcAve", "Julian", "Month", 
"Day", "DecYear", "MonthSeq", "SinDY", "CosDY", "Q", "LogQ", 
"yHat", "SE", "ConcHat"))



More information about the R-help mailing list