如何在绑定时保持 Dataframe 的行顺序?

lf5gs5x2  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(84)

我正在尝试创建一个函数,用于根据agricolae包中HSD.test的结果创建一个整洁的表格。HSD.test的输出是一个列表,其中治疗字母按平均值的降序排序。我想按T1、T2 ......等排列“平均值± sd字母”。尽管平均值和sd是这样排列的,字母没有按照顺序排列。2我不能用下面的函数完成下面的操作,因为字母没有正确地与处理对齐(下面每个数据框中的行名称)。3我到处搜索,并试图收集代码片段来制作这个函数。
如何在cbind ing期间保持df3行的顺序?

MyDf<-data.frame(treatment = rep(c('T1','T2','T3','T4'), each = 3),
                 p1 = c(28.5, 21.7, 23.0, 14.9, 10.6, 13.1, 41.8, 39.2, 28.0, 38.2, 40.4, 32.1), 
                 p2 = c(32, 37, 36, 23, 28, 22, 67, 52, 55, 18, 27, 17),
                 p3 = c(5.6, 3.7, 4.9, 7.1, 11.3, 14, 2.3, 5.4, 3.3, 11.6, 10.1, 12)
                 )

MeltMyDf<-melt(MyDf)

MyDfSE<-summarySE(MeltMyDf, measurevar = "value", groupvars = c("variable", "treatment"))

x <- as.character(unique(MyDfSE$variable))

MyModels<-sapply(x, function(my) {lm(value~treatment, data=MeltMyDf, variable==my)}, simplify=FALSE)

MyGroups<- lapply(MyModels, function(m) HSD.test((m), "treatment", alpha = 0.05, group = TRUE, console = FALSE, variable==variable))

#---- the function I have created ------------
make_HSD_table<-function(HSDlist){
  
  mycolnames<-names(HSDlist)
  mycolnumber<-length(mycolnames)
  
  df1<-as.data.frame(lapply(lapply(HSDlist, `[[`, 'means'), '[', 'value'))
  df1[order(row.names(df1)), ]
  df1<-round(df1, digits = 2)
  
  df2<-as.data.frame(lapply(lapply(HSDlist, `[[`, 'means'), '[', c('std')))
  df2[order(row.names(df2)), ]
  df2<-round(df2, digits = 2)
  
  df3<-as.data.frame(lapply(lapply(HSDlist, `[[`, 'groups'), '[', 'groups'))
  df3[order(row.names(df3)), ]

  myrownumber<-nrow(df1)
  pm_df<-data.frame(replicate(mycolnumber, rep('±', myrownumber)))
  
  my_table <- cbind(df1, pm_df, df2, df3)[order(c(seq_along(df1), seq_along(pm_df),seq_along(df2), seq_along(df3)))]
  
  
  my_table <- cbind(sapply(split.default(my_table, as.integer(gl(ncol(my_table), 4, ncol(my_table)))), function(x) do.call(paste, x)))
  
  colnames(my_table)<-mycolnames
  rownames(my_table)<-rownames(df1)

  write.table(my_table, 'my_HSD_table.csv', append = F, sep = ',', row.names = FALSE)
  
  return(my_table)
}

#-----------------------------------------
make_HSD_table(MyGroups)

   p1                p2               p3             
T1 "24.4 ± 3.61 a"   "35 ± 2.65 a"    "4.73 ± 0.96 a"
T2 "12.87 ± 2.16 ab" "24.33 ± 3.21 b" "10.8 ± 3.48 a"
T3 "36.33 ± 7.33 bc" "58 ± 7.94 bc"   "3.67 ± 1.58 b"
T4 "36.9 ± 4.3 c"    "20.67 ± 5.51 c" "11.23 ± 1 b"  

**As you can see, the values±sd are sorted according to the treatment. But the letters are not sorted and placed with wrong mean±sd!**
agxfikkp

agxfikkp1#

最后,我可以修改我的函数,从HSD.test输出mean ± sd grouping_letter表,该表按照 Dataframe 的顺序排序。

library(reshape2)
library(Rmisc)
library(agricolae)
library(dplyr)
library(stringr)

MyDf<-data.frame(treatment = rep(c('T1','T2','T3','T4'), each = 3),
                 p1 = c(28.5, 21.7, 23.0, 14.9, 10.6, 13.1, 41.8, 39.2, 28.0, 38.2, 40.4, 32.1), 
                 p2 = c(32, 37, 36, 23, 28, 22, 67, 52, 55, 18, 27, 17),
                 p3 = c(5.6, 3.7, 4.9, 7.1, 11.3, 14, 2.3, 5.4, 3.3, 11.6, 10.1, 12)
                 )

MeltMyDf<-melt(MyDf)

MyDfSE<-summarySE(MeltMyDf, measurevar = "value", groupvars = c("variable", "treatment"))

x <- as.character(unique(MyDfSE$variable))

MyModels<-sapply(x, function(my) {lm(value~treatment, data=MeltMyDf, variable==my)}, simplify=FALSE)

MyGroups<- lapply(MyModels, function(m) HSD.test((m), "treatment", alpha = 0.05, group = TRUE, console = FALSE, variable==variable))

现在,调用表的函数。它也将表导出为csv文件。

make_HSD_table<-function(HSDlist){
  
  mycolnames<-names(HSDlist)
  mycolnumber<-length(mycolnames)
  
  df1<-as.data.frame(lapply(lapply(HSDlist, `[[`, 'means'), '[', 'value'))
  df1[order(row.names(df1)), ]
  df1<-round(df1, digits = 2)
  
  df2<-as.data.frame(lapply(lapply(HSDlist, `[[`, 'means'), '[', c('std')))
  df2[order(row.names(df2)), ]
  df2<-round(df2, digits = 2)
  
  df3<-lapply(lapply(HSDlist, `[[`, 'groups'), '[', 'groups')
  df3<-Map(cbind, df3, my_row_names = lapply(df3, rownames))
  df3<-lapply(df3, function(df) {df[order(df$my_row_names), ]})
  df3<-lapply(df3, function(x) x[,1])
  
  myrownumber<-nrow(df1)
  pm_df<-data.frame(replicate(mycolnumber, rep('±', myrownumber)))
  
  my_table <- cbind(df1, pm_df, df2, df3)[order(c(seq_along(df1), seq_along(pm_df),seq_along(df2), seq_along(df3)))]
  
  
  my_table <- cbind(sapply(split.default(my_table, as.integer(gl(ncol(my_table), 4, ncol(my_table)))), function(x) do.call(paste, x)))
  
  colnames(my_table)<-mycolnames
  my_table<-data.frame(treatment = rownames(df1), my_table)

  
  write.table(my_table, 'my_HSD_table.csv', append = F, sep = ',', row.names = FALSE)
  
  return(my_table)
}

让我们从HSD.test输出创建该表。

make_HSD_table(MyGroups)

  treatment              p1              p2            p3
1        T1  24.4 ± 3.61 bc     35 ± 2.65 b 4.73 ± 0.96 b
2        T2  12.87 ± 2.16 c 24.33 ± 3.21 bc 10.8 ± 3.48 a
3        T3 36.33 ± 7.33 ab     58 ± 7.94 a 3.67 ± 1.58 b
4        T4    36.9 ± 4.3 a  20.67 ± 5.51 c   11.23 ± 1 a

正如您所看到的,这些字母已经被正确地分配给了相应的mean±sd列。

相关问题