重新排序由R中的字符列组成的数据框

时间:2018-07-18 15:31:08

标签: r knitr

这是我的数据框:

df <- structure(list(Species = c("AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"ERE", "ERE", "ERE", "ERE", "ERE", "ERE", "ERE", "ERE", "ERE", 
"ERE", "ERE", "ERE", "ERE", "ERE", "ERE", "ERE", "ERH", "ERH", 
"ERH", "ERH", "ERH", "NEA", "NEA", "NEA", "NEA", "NEA", "NEA", 
"NEA", "NEA", "NEA", "NEA", "NEA", "NEA", "NEA", "NEA", "NEA", 
"NEA", "NEA", "NEA"), Specimen = c("KP 29286", "KP 34725", "LH3", 
"LH3", "LH3", "LH3", "LH3", "LH3", "LH6", "LH6", "LH6", "LH6", 
"LH6", "MLD11", "MLD11", "MLD43", "Sts24", "Sts24", "Sts24a", 
"Sts50", "Sts52a/b", "Sts52a/b", "Sts52a/b", "Sts52a/b", "Sts52a/b", 
"Sts52a/b", "Sts52a/b", "Stw132", "Stw151", "Stw151", "Stw151", 
"Stw151", "Stw183", "Stw425", "LH2", "Sts24a", "Stw 284", "Stw 285", 
"Stw 37", "Stw 402", "Stw 284", "KNM-ER 1590", "KNM-ER 1590", 
"Sangiran 4", "KNM-WT 15000", "KNM-WT 15000", "KNM-WT 15000", 
"KNM-WT 15000", "KNM-WT 15000", "KNM-WT 15000", "KNM-WT 15000", 
"KNM-WT 15000", "KNM-WT 15000", "KNM-WT 15000", "KNM-WT 15000", 
"KNM-WT 15000", "KNM-WT 15000", "OH6", "SK 74b", "KNM-ER 808", 
"KNM-ER 808", "SK27", "1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18"), Tooth = c("LC", 
"LI1", "UP3", "UC", "UI1", "UI2", "UI2", "UI1", "UP3", "UI2", 
"UC", "UI2", "LC", "UI2", "UC", "UI1", "LI2", "LI1", "UI2", "LC", 
"LI1", "LC", "UI2", "UC", "LI2", "UI1", "UC", "LC", "UI2", "LI1", 
"UI1", "LI2", "UC", "LI2", "LI1", "UI1", "UM2", "LM2", "UM3", 
"UM1", "UM2", "UC", "UI1", "UC", "LI2", "UI1", "UI2", "LC", "UI2", 
"UI2", "UI1", "LI1", "LI2", "UI1", "LC", "LC", "UC", "UI2", "LI1", 
"UI2", "UI1", "UC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", 
"LC", "LC", "LI1", "LI1", "LI1", "LI2", "LI2", "LI2", "LI2", 
"LI2", "LI2"), cod = c("D", "D", "21", "21", "21", "21", "D", 
"D", "21", "21", "21", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "28", "D", "23", "23", "23", "23", "60", "D", "D", 
"D", "20", "20", "20", "20", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "21", "28", "D", "D", "D", "G", "G", "G", "G", "G", 
"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G"
)), .Names = c("Species", "Specimen", "Tooth", "cod"), row.names = c(NA, 
80L), class = "data.frame")

我如何获得这样的表: enter image description here

我希望首先按物种重新排序,然后按牙齿分组,计算每个物种/牙齿有多少颗牙齿,列出标本(用逗号分隔),以及列出代码(鳕鱼)。

1 个答案:

答案 0 :(得分:1)

kableExtra-0.9.0需要valign="top"-0.8.0尚未进行垂直对齐。

library(dplyr)
library(knitr)
library(kableExtra)

group_by(df, Species, Tooth) %>%
  summarize(
    n=n(),
    Specimen=paste(Specimen, collapse=", "),
    cod=paste(cod, collapse=", ")
  ) %>%
  kable(format="html", align="c") %>%
  kable_styling(full_width = FALSE) %>%
  column_spec(1, bold = TRUE) %>%
  collapse_rows(columns = 1, valign = "top")

<table class="table" style="width: auto !important; margin-left: auto; margin-right: auto;">
 <thead>
  <tr>
   <th style="text-align:left;"> Species </th>
   <th style="text-align:left;"> Tooth </th>
   <th style="text-align:left;"> n </th>
   <th style="text-align:left;"> Specimen </th>
   <th style="text-align:left;"> cod </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;font-weight: bold;vertical-align: top !important;" rowspan="11"> AUS </td>
   <td style="text-align:left;"> LC </td>
   <td style="text-align:left;"> 5 </td>
   <td style="text-align:left;"> KP 29286, LH6, Sts50, Sts52a/b, Stw132 </td>
   <td style="text-align:left;"> D, D, D, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LI1 </td>
   <td style="text-align:left;"> 5 </td>
   <td style="text-align:left;"> KP 34725, Sts24, Sts52a/b, Stw151, LH2 </td>
   <td style="text-align:left;"> D, D, D, D, 28 </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LI2 </td>
   <td style="text-align:left;"> 4 </td>
   <td style="text-align:left;"> Sts24, Sts52a/b, Stw151, Stw425 </td>
   <td style="text-align:left;"> D, D, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LM2 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> Stw 285 </td>
   <td style="text-align:left;"> 23 </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UC </td>
   <td style="text-align:left;"> 6 </td>
   <td style="text-align:left;"> LH3, LH6, MLD11, Sts52a/b, Sts52a/b, Stw183 </td>
   <td style="text-align:left;"> 21, 21, D, D, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UI1 </td>
   <td style="text-align:left;"> 6 </td>
   <td style="text-align:left;"> LH3, LH3, MLD43, Sts52a/b, Stw151, Sts24a </td>
   <td style="text-align:left;"> 21, D, D, D, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UI2 </td>
   <td style="text-align:left;"> 8 </td>
   <td style="text-align:left;"> LH3, LH3, LH6, LH6, MLD11, Sts24a, Sts52a/b, Stw151 </td>
   <td style="text-align:left;"> 21, D, 21, D, D, D, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UM1 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> Stw 402 </td>
   <td style="text-align:left;"> 23 </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UM2 </td>
   <td style="text-align:left;"> 2 </td>
   <td style="text-align:left;"> Stw 284, Stw 284 </td>
   <td style="text-align:left;"> 23, 60 </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UM3 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> Stw 37 </td>
   <td style="text-align:left;"> 23 </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UP3 </td>
   <td style="text-align:left;"> 2 </td>
   <td style="text-align:left;"> LH3, LH6 </td>
   <td style="text-align:left;"> 21, 21 </td>
  </tr>
  <tr>
   <td style="text-align:left;font-weight: bold;vertical-align: top !important;" rowspan="6"> ERE </td>
   <td style="text-align:left;"> LC </td>
   <td style="text-align:left;"> 3 </td>
   <td style="text-align:left;"> KNM-WT 15000, KNM-WT 15000, KNM-WT 15000 </td>
   <td style="text-align:left;"> 20, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LI1 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> KNM-WT 15000 </td>
   <td style="text-align:left;"> D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LI2 </td>
   <td style="text-align:left;"> 2 </td>
   <td style="text-align:left;"> KNM-WT 15000, KNM-WT 15000 </td>
   <td style="text-align:left;"> 20, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UC </td>
   <td style="text-align:left;"> 3 </td>
   <td style="text-align:left;"> KNM-ER 1590, Sangiran 4, KNM-WT 15000 </td>
   <td style="text-align:left;"> D, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UI1 </td>
   <td style="text-align:left;"> 4 </td>
   <td style="text-align:left;"> KNM-ER 1590, KNM-WT 15000, KNM-WT 15000, KNM-WT 15000 </td>
   <td style="text-align:left;"> D, 20, D, D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UI2 </td>
   <td style="text-align:left;"> 3 </td>
   <td style="text-align:left;"> KNM-WT 15000, KNM-WT 15000, KNM-WT 15000 </td>
   <td style="text-align:left;"> 20, D, D </td>
  </tr>
  <tr>
   <td style="text-align:left;font-weight: bold;vertical-align: top !important;" rowspan="4"> ERH </td>
   <td style="text-align:left;"> LI1 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> SK 74b </td>
   <td style="text-align:left;"> 28 </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UC </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> SK27 </td>
   <td style="text-align:left;"> D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UI1 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> KNM-ER 808 </td>
   <td style="text-align:left;"> D </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> UI2 </td>
   <td style="text-align:left;"> 2 </td>
   <td style="text-align:left;"> OH6, KNM-ER 808 </td>
   <td style="text-align:left;"> 21, D </td>
  </tr>
  <tr>
   <td style="text-align:left;font-weight: bold;vertical-align: top !important;" rowspan="3"> NEA </td>
   <td style="text-align:left;"> LC </td>
   <td style="text-align:left;"> 9 </td>
   <td style="text-align:left;"> 1, 2, 3, 4, 5, 6, 7, 8, 9 </td>
   <td style="text-align:left;"> G, G, G, G, G, G, G, G, G </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LI1 </td>
   <td style="text-align:left;"> 3 </td>
   <td style="text-align:left;"> 10, 11, 12 </td>
   <td style="text-align:left;"> G, G, G </td>
  </tr>
  <tr>
   
   <td style="text-align:left;"> LI2 </td>
   <td style="text-align:left;"> 6 </td>
   <td style="text-align:left;"> 13, 14, 15, 16, 17, 18 </td>
   <td style="text-align:left;"> G, G, G, G, G, G </td>
  </tr>
</tbody>
</table>