当列表包含多个项目(如方差分析、事后分析等)时,如何将嵌套列表转换为数据框

问题描述 投票:0回答:1

我使用R中的doebioresearch包进行了split.plot分析。多个性状的结果以嵌套列表的形式生成,输出文件的整体结构是多层的。我想将此输出以表格形式保存在 Excel 工作表中,但在将列表结构转换为数据框架时出现问题。

example_split<-splitplot(data=splitdata[4:5], 
          block=splitdata$Replication, main.plot=splitdata$Date_of_Sowing, 
          sub.plot=splitdata$Varities, mean.comparison.test=3)#3 for tuckey
class(example_split)
str(example_split)

现在我想将这个 example_split 输出保存在 Excel 中。为此我需要将其转换为数据框。 我试过:

data.frame(do.call(rbind.data.frame, example_split))

它显示的错误是

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ‘"htest"’ to a data.frame
r dataframe nested-lists
1个回答
0
投票

复杂的列表不适合转换为数据框。

您可以尝试

broom
包中的 tidy 函数,它可以将某些 R 分析的结果转换为数据框,然后可以 保存到 Excel 文件中。但是,您需要选择要保存输出的哪些部分。你不能只整理整个事情。

library(broom)

tidy(example_split$Yield[[1]][[1]])
# A tibble: 6 × 6
  term                  df   sumsq  meansq statistic p.value
  <chr>              <int>   <dbl>   <dbl>     <dbl>   <dbl>
1 block                  2  61557.  30779.   0.188    0.842 
2 main.plot              1    187.    187.   0.00114  0.976 
3 Ea                     2 328176. 164088.  NA       NA     
4 sub.plot               5 510277. 102055.   2.99     0.0356
5 main.plot:sub.plot     5 125447.  25089.   0.735    0.606 
6 Eb                    20 682307.  34115.  NA       NA 

tidy(example_split$Yield[[1]][[1]])
 # A tibble: 6 × 6
  term                  df   sumsq  meansq statistic p.value
  <chr>              <int>   <dbl>   <dbl>     <dbl>   <dbl>
1 block                  2  61557.  30779.   0.188    0.842 
2 main.plot              1    187.    187.   0.00114  0.976 
3 Ea                     2 328176. 164088.  NA       NA     
4 sub.plot               5 510277. 102055.   2.99     0.0356
5 main.plot:sub.plot     5 125447.  25089.   0.735    0.606 
6 Eb                    20 682307.  34115.  NA       NA     
© www.soinside.com 2019 - 2024. All rights reserved.