我有一份数据框的摘要列表,我想把它们全部写入一个文件中,这样我就可以得到一个包含每个数据框调用 summary()
输出的单个文本文件。
我尝试了以下方法:
dflist = lapply(1:4, function(x){df = data.frame(SID=paste("S",1:10,sep=""),matrix(runif(100),nrow=10,ncol=10))})
sumlist = lapply(dflist, function(df){summary(df)})
lapply(sumlist, function(i){write(i,"all_data_summary.txt",append=T )})
它有点起作用,但它将每个变量的摘要级别写入新行,并且不包括变量名称,就像这样:
S1 :1
S10 :1
S2 :1
S3 :1
S4 :1
S5 :1
(Other):4
Min. :0.1557
1st Qu.:0.2284
Median :0.5109
Mean :0.4707
3rd Qu.:0.6471
Max. :0.9509
NA
Min. :0.05512
1st Qu.:0.14310
Median :0.20889
Mean :0.41891
3rd Qu.:0.78261
Max. :0.97565
NA
Min. :0.01937
1st Qu.:0.23876
Median :0.57103
Mean :0.50806
3rd Qu.:0.70491
Max. :0.95610
NA
Min. :0.0212
1st Qu.:0.3445
Median :0.5634
Mean :0.5554
3rd Qu.:0.7484
Max. :0.9797
I would like the output to be of the form:
List Element 1
SID X1 X2 X3 X4
S1 :1 Min. :0.1557 Min. :0.05512 Min. :0.01937 Min. :0.0212
S10 :1 1st Qu.:0.2284 1st Qu.:0.14310 1st Qu.:0.23876 1st Qu.:0.3445
S2 :1 Median :0.5109 Median :0.20889 Median :0.57103 Median :0.5634
S3 :1 Mean :0.4707 Mean :0.41891 Mean :0.50806 Mean :0.5554
S4 :1 3rd Qu.:0.6471 3rd Qu.:0.78261 3rd Qu.:0.70491 3rd Qu.:0.7484
S5 :1 Max. :0.9509 Max. :0.97565 Max. :0.95610 Max. :0.9797
(Other):4
List Element 2
SID X1 X2 X3 X4
S1 :1 Min. :0.1557 Min. :0.05512 Min. :0.01937 Min. :0.0212
S10 :1 1st Qu.:0.2284 1st Qu.:0.14310 1st Qu.:0.23876 1st Qu.:0.3445
S2 :1 Median :0.5109 Median :0.20889 Median :0.57103 Median :0.5634
S3 :1 Mean :0.4707 Mean :0.41891 Mean :0.50806 Mean :0.5554
S4 :1 3rd Qu.:0.6471 3rd Qu.:0.78261 3rd Qu.:0.70491 3rd Qu.:0.7484
S5 :1 Max. :0.9509 Max. :0.97565 Max. :0.95610 Max. :0.9797
有什么想法吗? 谢谢, Davy.