将摘要转换为data.frame

sbdsn5lh  于 2023-03-27  发布在  其他
关注(0)|答案(3)|浏览(120)

我有这个admission_table包含ADMITGREGPARANK

> head(admission_table)
  ADMIT GRE  GPA RANK
1     0 380 3.61    3
2     1 660 3.67    3
3     1 800 4.00    1
4     1 640 3.19    4
5     0 520 2.93    4
6     1 760 3.00    2

我正在尝试将此表的摘要转换为data.frame。我希望将ADMITGREGPARANK作为列标题。

> summary(admission_table)
     ADMIT             GRE             GPA             RANK      
 Min.   :0.0000   Min.   :220.0   Min.   :2.260   Min.   :1.000  
 1st Qu.:0.0000   1st Qu.:520.0   1st Qu.:3.130   1st Qu.:2.000  
 Median :0.0000   Median :580.0   Median :3.395   Median :2.000  
 Mean   :0.3175   Mean   :587.7   Mean   :3.390   Mean   :2.485  
 3rd Qu.:1.0000   3rd Qu.:660.0   3rd Qu.:3.670   3rd Qu.:3.000  
 Max.   :1.0000   Max.   :800.0   Max.   :4.000   Max.   :4.000  

 > as.data.frame(summary(admission_table))
   Var1      Var2             Freq
1           ADMIT Min.   :0.0000  
2           ADMIT 1st Qu.:0.0000  
3           ADMIT Median :0.0000  
4           ADMIT Mean   :0.3175  
5           ADMIT 3rd Qu.:1.0000  
6           ADMIT Max.   :1.0000  
7             GRE  Min.   :220.0  
8             GRE  1st Qu.:520.0  
9             GRE  Median :580.0  
10            GRE  Mean   :587.7  
11            GRE  3rd Qu.:660.0  
12            GRE  Max.   :800.0  
13            GPA  Min.   :2.260  
14            GPA  1st Qu.:3.130  
15            GPA  Median :3.395  
16            GPA  Mean   :3.390  
17            GPA  3rd Qu.:3.670  
18            GPA  Max.   :4.000  
19           RANK  Min.   :1.000  
20           RANK  1st Qu.:2.000    
21           RANK  Median :2.000  
22           RANK  Mean   :2.485  
23           RANK  3rd Qu.:3.000  
24           RANK  Max.   :4.000

当我试图转换为data.frame时,这是我得到的唯一结果。我希望 Dataframe 具有与汇总表相同的精确输出,因为在此之后,我希望使用以下代码行将其插入Oracle数据库:

dbWriteTable(connection,name="SUM_ADMISSION_TABLE",value=as.data.frame(summary(admission_table)),row.names = FALSE, overwrite = TRUE ,append = FALSE)

有什么办法可以做到吗?

5uzkadbs

5uzkadbs1#

你可以考虑unclass,我想:

data.frame(unclass(summary(mydf)), check.names = FALSE, stringsAsFactors = FALSE)
#              ADMIT             GRE             GPA            RANK
# 1 Min.   :0.0000   Min.   :380.0   Min.   :2.930   Min.   :1.000  
# 2 1st Qu.:0.2500   1st Qu.:550.0   1st Qu.:3.047   1st Qu.:2.250  
# 3 Median :1.0000   Median :650.0   Median :3.400   Median :3.000  
# 4 Mean   :0.6667   Mean   :626.7   Mean   :3.400   Mean   :2.833  
# 5 3rd Qu.:1.0000   3rd Qu.:735.0   3rd Qu.:3.655   3rd Qu.:3.750  
# 6 Max.   :1.0000   Max.   :800.0   Max.   :4.000   Max.   :4.000  
str(.Last.value)
# 'data.frame': 6 obs. of  4 variables:
#  $     ADMIT: chr  "Min.   :0.0000  " "1st Qu.:0.2500  " "Median :1.0000  " "Mean   :0.6667  " ...
#  $      GRE : chr  "Min.   :380.0  " "1st Qu.:550.0  " "Median :650.0  " "Mean   :626.7  " ...
#  $      GPA : chr  "Min.   :2.930  " "1st Qu.:3.047  " "Median :3.400  " "Mean   :3.400  " ...
#  $      RANK: chr  "Min.   :1.000  " "1st Qu.:2.250  " "Median :3.000  " "Mean   :2.833  " ...

请注意,在名称和值中都有大量多余的空格。
但是,执行以下操作可能就足够了:

do.call(cbind, lapply(mydf, summary))
#          ADMIT   GRE   GPA  RANK
# Min.    0.0000 380.0 2.930 1.000
# 1st Qu. 0.2500 550.0 3.048 2.250
# Median  1.0000 650.0 3.400 3.000
# Mean    0.6667 626.7 3.400 2.833
# 3rd Qu. 1.0000 735.0 3.655 3.750
# Max.    1.0000 800.0 4.000 4.000
jm81lzqq

jm81lzqq2#

输出 Dataframe 的另一种方式是:

as.data.frame(apply(mydf, 2, summary))

仅选择数值列时有效。
如果有NA的列,它可能会抛出Error in dimnames(x)。值得在没有as.data.frame()函数的情况下首先检查一下。

scyqe7ek

scyqe7ek3#

这些解决方案实际上都没有捕获summary函数的输出。tidy()函数从summary对象中提取元素并创建一个平淡无奇的data.frame,因此它不保留其他功能或格式。
如果要在数据框中精确显示汇总函数的输出,可以执行以下操作:

output <- capture.output(summary(thisModel), file=NULL,append=FALSE)
output_df <- as.data.frame(output)

这保留了所有新行,并且适合于写入XLSX等,这将导致输出适当地跨行间隔。
如果您希望将此输出折叠到单个单元格中,可以执行以下操作:

output_collapsed <- paste0(output, sep="", collapse="\n")
output_df <- as.data.frame(output_collapsed)

相关问题