繁体   English   中英

将前 3 行设置为 R 表中的标题

[英]Set first 3 rows as Headers in R table

我有一个大型数据集,我为每个站点创建了一个子集(数据集包含多个站点)。 我想在 R markdown 中为每个站点制作一个表格,但前三行作为标题数据。 这是因为我想使用KableExtra包中的add_header_above函数。 现在,当我在我的 Markdown 文档中调用这个函数时,该文件不会编织,我猜是因为表格的排序方式(标题数据现在作为表格中的变量)。

这是我使用的代码,将子集置于所需的格式中。 我知道它不整洁,但这是我需要表格的格式。下面是这个转换子集的 dput。

veg.temp <- veg.temp %>% select(Code, Jaar, everything()) %>% arrange(Code)
   veg.temp <- t(veg.temp)
    colnames(veg.temp) <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13","14", "15",
                            "16", "17", "18", "19", "20", "21", "22", "23", "24")

我制作了人为的列名 ( 1:24 ),但我希望前三行是列名(Code、Jaar、Opnamenummer)。 有没有办法做到这一点? 我的表现在看起来像这样(只有前几行): 在此处输入图片说明

子集数据集的 Dput,包括上面的代码:

structure(c("de Kom", "KoQ001", "2003", " 3921", " 0", "0", "0", 
" 0", "0", "0", "0", "0", "0", " 4", " 4", "0", "1", "0", "0", 
"0", " 0", " 1", "0", "0", "0", "0", " 0", "0", " 0", "0", "43", 
"0", "0", "0", "0", "0", "0", "0", "0", " 4", "1", "4", "4", 
"1", " 8", "0", "0", "0", " 0", "43", " 0", "0", "0", "1", "4", 
"0", "1", "1", "0", "0", " 4", "0", "0", "0", "0", "0", " 1", 
" 0", "4", "1", "0", "0", "0", "0", "0", "0", "18", "1", "0", 
"0", "0", "0", "0", "0", "1", "0", "1", "0", "0", "0", "0", "1", 
"1", "0", "0", "de Kom", "KoQ001", "2006", " 7014", " 0", "0", 
"0", "18", "0", "0", "0", "0", "0", "68", " 1", "0", "0", "0", 
"1", "0", " 0", " 0", "1", "0", "0", "0", " 0", "0", " 0", "0", 
"43", "0", "0", "0", "0", "0", "0", "0", "0", " 4", "0", "1", 
"4", "0", " 0", "0", "0", "0", "56", " 0", " 0", "0", "0", "1", 
"1", "1", "1", "0", "0", "0", " 4", "0", "0", "0", "0", "0", 
" 8", " 0", "4", "1", "0", "0", "0", "0", "0", "1", " 8", "1", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", 
"1", "1", "0", "0", "de Kom", "KoQ001", "2009", "10257", " 0", 
"0", "0", " 0", "0", "0", "0", "0", "0", "68", " 1", "0", "0", 
"0", "0", "0", " 0", " 0", "0", "0", "0", "0", " 0", "0", " 0", 
"0", "18", "0", "0", "0", "0", "0", "0", "0", "0", " 4", "0", 
"1", "4", "0", " 1", "0", "0", "0", "81", " 0", " 0", "0", "0", 
"1", "0", "0", "0", "0", "0", "0", " 1", "0", "0", "0", "0", 
"0", " 4", " 0", "4", "0", "0", "0", "0", "1", "0", "1", "31", 
"1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "1", "0", "de Kom", "KoQ001", "2012", "11259", 
" 0", "0", "0", " 0", "0", "0", "0", "0", "0", "43", " 4", "0", 
"0", "0", "1", "0", " 0", " 0", "0", "0", "0", "0", " 0", "0", 
" 0", "0", "18", "0", "0", "0", "0", "4", "0", "0", "0", " 4", 
"0", "1", "1", "0", " 0", "0", "0", "0", " 0", "56", " 0", "0", 
"1", "1", "0", "1", "0", "0", "0", "0", " 8", "0", "0", "0", 
"0", "0", " 8", "18", "8", "1", "0", "0", "0", "0", "0", "1", 
" 8", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "1", "1", "1", "0", "0", "de Kom", "KoQ002", "2003", 
" 3922", " 0", "1", "0", " 0", "0", "0", "0", "0", "0", " 4", 
" 4", "0", "0", "0", "0", "0", " 0", " 1", "0", "0", "0", "0", 
" 0", "0", " 1", "1", "43", "0", "0", "0", "0", "0", "0", "0", 
"0", " 4", "0", "1", "4", "1", " 4", "0", "0", "0", " 0", "43", 
" 0", "0", "0", "1", "4", "1", "0", "0", "0", "0", " 8", "0", 
"0", "0", "0", "1", " 1", " 0", "4", "1", "0", "0", "0", "1", 
"0", "0", "18", "0", "0", "0", "0", "0", "1", "0", "1", "1", 
"1", "1", "0", "0", "1", "1", "1", "0", "0", "de Kom", "KoQ002", 
"2006", " 7016", " 0", "0", "0", " 0", "0", "0", "0", "0", "0", 
" 8", " 1", "0", "1", "0", "1", "0", " 0", " 4", "0", "0", "0", 
"0", " 0", "0", " 0", "0", "31", "0", "0", "0", "1", "0", "0", 
"0", "0", " 8", "1", "1", "4", "1", "18", "0", "0", "0", "56", 
" 0", " 0", "0", "1", "1", "1", "4", "0", "1", "0", "0", " 8", 
"0", "0", "0", "0", "0", " 4", " 0", "4", "4", "0", "0", "0", 
"0", "0", "1", " 8", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "1", "1", "0", "0", "1", "1", "4", "0", "0", "de Kom", "KoQ002", 
"2009", " 9778", " 0", "0", "0", " 0", "1", "0", "0", "0", "0", 
"18", " 1", "0", "0", "0", "1", "0", " 0", " 1", "0", "0", "0", 
"0", " 1", "0", " 0", "0", " 8", "0", "0", "0", "0", "4", "0", 
"0", "0", "18", "1", "1", "1", "1", " 8", "1", "0", "0", " 0", 
"68", " 0", "0", "1", "1", "1", "1", "0", "1", "0", "0", "18", 
"0", "0", "0", "0", "0", "18", " 0", "8", "1", "0", "0", "0", 
"0", "0", "1", " 8", "1", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "de Kom", "KoQ002", 
"2012", "11372", " 0", "0", "0", " 0", "0", "0", "0", "0", "0", 
"31", " 4", "0", "1", "0", "1", "0", " 0", " 1", "0", "0", "0", 
"0", " 1", "0", " 0", "0", " 3", "0", "0", "0", "0", "4", "0", 
"1", "0", " 4", "0", "1", "1", "0", " 1", "1", "0", "0", " 0", 
"93", " 0", "0", "1", "1", "4", "1", "0", "1", "0", "1", " 8", 
"0", "0", "0", "0", "0", " 4", " 0", "4", "1", "0", "0", "1", 
"0", "0", "1", " 1", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "de Kom", "KoQ003", 
"2003", " 3923", " 0", "0", "1", " 0", "0", "1", "0", "0", "0", 
" 0", " 4", "0", "0", "8", "0", "0", "31", " 4", "0", "0", "0", 
"4", " 8", "0", " 2", "1", " 0", "0", "0", "0", "0", "0", "0", 
"1", "0", " 4", "0", "0", "0", "0", " 0", "0", "0", "0", " 0", 
" 4", " 0", "0", "0", "0", "1", "0", "0", "0", "0", "0", " 0", 
"1", "1", "1", "4", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 1", "0", "4", "0", "0", "0", "4", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "de Kom", "KoQ003", 
"2006", " 7017", " 0", "0", "1", " 0", "0", "1", "0", "1", "0", 
" 0", " 4", "1", "4", "8", "0", "0", " 8", " 4", "0", "0", "0", 
"4", " 8", "0", " 8", "1", " 0", "0", "1", "0", "1", "0", "1", 
"0", "0", " 4", "0", "0", "1", "0", " 0", "0", "0", "0", "18", 
" 0", " 0", "0", "0", "0", "1", "0", "0", "0", "0", "0", " 0", 
"4", "1", "1", "4", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 1", "0", "4", "0", "0", "0", "4", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "de Kom", "KoQ003", 
"2009", " 9775", " 0", "0", "1", " 0", "0", "0", "0", "0", "0", 
" 1", " 4", "1", "4", "4", "0", "0", " 8", " 1", "0", "0", "1", 
"0", " 1", "0", " 8", "0", " 0", "0", "0", "0", "0", "0", "1", 
"0", "0", " 1", "0", "0", "1", "0", " 0", "0", "0", "0", " 0", 
"31", " 0", "0", "1", "1", "1", "1", "0", "0", "0", "0", " 0", 
"1", "0", "1", "1", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 1", "0", "1", "0", "0", "0", "8", "1", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "de Kom", "KoQ003", 
"2012", "11374", " 0", "1", "0", " 0", "0", "0", "0", "0", "1", 
" 0", " 4", "0", "1", "4", "0", "0", " 8", " 4", "0", "1", "0", 
"0", " 1", "0", " 8", "0", " 0", "0", "0", "0", "0", "0", "0", 
"1", "0", " 1", "0", "0", "1", "0", " 0", "0", "0", "0", " 0", 
"31", " 0", "0", "1", "0", "1", "1", "0", "0", "0", "0", " 0", 
"1", "0", "1", "1", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 1", "0", "1", "0", "0", "0", "1", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "de Kom", "KoQ004", 
"2003", " 3924", " 0", "0", "1", " 0", "0", "0", "0", "0", "0", 
" 0", " 4", "1", "1", "8", "0", "0", "18", " 4", "0", "0", "0", 
"4", " 8", "0", " 4", "1", " 0", "0", "0", "1", "0", "0", "0", 
"1", "0", " 4", "0", "0", "0", "0", " 0", "0", "0", "0", " 0", 
"18", " 0", "0", "0", "1", "1", "0", "0", "0", "0", "0", " 0", 
"1", "0", "0", "4", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 1", "0", "4", "0", "0", "0", "4", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "1", "de Kom", "KoQ004", 
"2006", " 7015", " 0", "0", "1", " 0", "0", "0", "0", "1", "0", 
" 0", " 4", "1", "4", "4", "0", "4", "43", " 4", "0", "0", "0", 
"4", " 0", "0", " 8", "1", " 0", "0", "0", "0", "1", "0", "1", 
"0", "0", " 4", "0", "0", "0", "0", " 0", "0", "1", "0", "31", 
" 0", " 0", "0", "0", "0", "1", "0", "0", "0", "0", "0", " 0", 
"4", "0", "1", "4", "1", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 3", "0", "4", "0", "0", "0", "1", "0", "0", 
"0", "0", "0", "0", "1", "1", "0", "1", "0", "1", "de Kom", "KoQ004", 
"2009", "10255", " 0", "0", "1", " 0", "0", "1", "0", "0", "0", 
" 0", " 4", "1", "1", "8", "0", "0", "18", " 8", "0", "0", "1", 
"0", "18", "0", "18", "1", " 0", "0", "0", "0", "0", "0", "1", 
"0", "0", " 4", "0", "0", "0", "0", " 0", "0", "1", "0", "31", 
" 0", " 0", "1", "1", "1", "2", "1", "0", "0", "0", "0", " 0", 
"1", "1", "1", "4", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", "18", "0", "4", "1", "0", "1", "1", "1", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "1", "de Kom", "KoQ004", 
"2012", "11261", " 0", "0", "1", " 0", "0", "1", "0", "0", "1", 
" 0", " 1", "0", "1", "4", "0", "0", "31", "18", "0", "1", "1", 
"0", "43", "0", " 8", "1", " 0", "1", "0", "0", "0", "0", "1", 
"0", "0", " 1", "0", "0", "1", "0", " 0", "0", "1", "0", " 8", 
" 0", " 0", "1", "0", "1", "1", "1", "0", "0", "0", "0", " 0", 
"1", "0", "1", "1", "0", " 0", " 0", "0", "0", "0", "0", "0", 
"0", "0", "0", " 8", "0", "1", "1", "0", "0", "8", "0", "0", 
"0", "0", "0", "0", "0", "1", "1", "1", "0", "1", "de Kom", "KoQ005", 
"2003", " 3925", " 0", "0", "0", " 0", "0", "0", "0", "0", "0", 
" 4", "56", "0", "0", "0", "0", "0", " 1", " 4", "0", "0", "0", 
"0", " 0", "0", " 0", "0", " 0", "0", "0", "0", "0", "0", "0", 
"0", "0", " 4", "0", "0", "0", "0", " 0", "0", "0", "0", " 0", 
"93", " 0", "0", "0", "1", "1", "0", "0", "0", "0", "0", " 4", 
"0", "0", "0", "0", "0", " 0", " 0", "1", "0", "0", "0", "0", 
"1", "1", "0", " 0", "4", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "de Kom", "KoQ005", 
"2006", " 7019", " 0", "1", "0", " 0", "0", "0", "0", "0", "0", 
"18", "43", "0", "1", "0", "0", "0", " 1", " 0", "0", "0", "0", 
"0", " 4", "0", " 0", "1", " 0", "0", "0", "0", "0", "0", "1", 
"0", "1", " 4", "0", "0", "1", "0", " 1", "0", "0", "0", "68", 
" 0", " 0", "0", "0", "1", "1", "1", "0", "0", "1", "0", " 8", 
"1", "0", "0", "1", "0", " 0", " 0", "4", "0", "0", "0", "0", 
"0", "1", "0", " 0", "4", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "de Kom", "KoQ005", 
"2009", " 9777", " 0", "1", "0", " 0", "0", "0", "2", "1", "0", 
"68", " 4", "0", "0", "0", "0", "0", " 0", " 0", "0", "0", "0", 
"0", " 4", "0", " 0", "1", " 2", "0", "0", "0", "0", "0", "1", 
"0", "0", " 4", "0", "0", "0", "0", " 0", "0", "0", "0", "88", 
" 0", " 0", "0", "1", "2", "2", "2", "0", "0", "3", "0", " 8", 
"0", "0", "0", "1", "0", " 0", " 0", "4", "0", "1", "0", "1", 
"0", "2", "0", " 0", "4", "0", "0", "1", "0", "1", "0", "0", 
"0", "0", "1", "0", "0", "4", "0", "0", "0", "0", "de Kom", "KoQ005", 
"2012", "11373", "18", "0", "0", " 0", "0", "0", "1", "0", "0", 
"56", " 8", "0", "0", "0", "1", "0", " 1", " 1", "0", "0", "0", 
"0", " 0", "0", " 0", "0", " 2", "0", "0", "0", "0", "0", "0", 
"1", "0", " 1", "0", "0", "0", "0", " 1", "0", "0", "1", " 0", 
" 0", "93", "0", "0", "1", "1", "1", "0", "0", "1", "0", " 8", 
"0", "0", "0", "0", "0", " 0", " 0", "1", "0", "0", "1", "1", 
"0", "1", "0", " 0", "1", "0", "1", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "de Kom", "KoQ006", 
"2003", " 3926", " 0", "1", "0", " 0", "0", "0", "0", "0", "0", 
" 4", "43", "0", "0", "0", "0", "0", " 1", " 0", "0", "0", "0", 
"0", " 1", "0", " 0", "0", " 1", "0", "0", "0", "0", "0", "0", 
"0", "0", " 4", "0", "0", "1", "0", " 1", "0", "0", "0", " 0", 
"93", " 0", "0", "0", "1", "1", "1", "0", "0", "0", "0", " 1", 
"0", "0", "0", "1", "0", " 0", " 0", "4", "0", "0", "0", "0", 
"0", "0", "0", " 0", "4", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "de Kom", "KoQ006", 
"2006", " 7018", " 0", "1", "0", " 0", "0", "0", "0", "0", "0", 
" 4", "81", "0", "0", "0", "4", "0", " 0", " 0", "0", "0", "0", 
"0", " 1", "1", " 0", "1", "31", "0", "0", "0", "0", "0", "0", 
"0", "0", " 4", "0", "0", "1", "0", " 0", "0", "0", "0", "81", 
" 0", " 0", "0", "0", "1", "1", "0", "0", "0", "0", "0", " 4", 
"1", "0", "0", "1", "0", " 0", " 0", "1", "0", "0", "0", "0", 
"0", "0", "0", " 0", "4", "0", "0", "1", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "de Kom", "KoQ006", 
"2009", "10256", " 0", "0", "0", " 0", "0", "0", "0", "0", "0", 
"68", " 8", "0", "0", "0", "1", "0", " 0", " 0", "0", "0", "0", 
"0", " 1", "0", " 0", "0", " 8", "0", "0", "0", "0", "0", "0", 
"0", "0", " 4", "0", "0", "1", "0", " 0", "0", "0", "0", "81", 
" 0", " 0", "0", "0", "1", "0", "0", "0", "0", "1", "0", " 4", 
"0", "0", "0", "0", "0", " 0", " 0", "1", "0", "0", "0", "0", 
"0", "0", "0", " 0", "1", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "de Kom", "KoQ006", 
"2012", "11260", " 0", "0", "0", " 0", "0", "0", "0", "0", "0", 
"68", " 4", "0", "0", "0", "1", "0", " 0", " 1", "0", "0", "0", 
"0", " 0", "0", " 0", "0", "18", "0", "0", "0", "0", "0", "0", 
"0", "0", " 1", "0", "0", "1", "0", " 0", "0", "0", "0", "31", 
" 0", " 0", "0", "1", "1", "0", "0", "0", "0", "1", "0", " 4", 
"0", "0", "0", "0", "0", " 0", " 0", "1", "0", "0", "0", "0", 
"0", "0", "0", " 0", "1", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "1", "0", "0", "0"), .Dim = c(95L, 
24L), .Dimnames = list(c("Terrein", "Code", "Jaar", "Opnamenummer", 
"Agrostis.capillaris6", "Aira.praecox6", "Ammophila.arenaria6", 
"Avenula.pubescens6", "Botrychium.lunaria6", "Brachythecium.albicans9", 
"Brachythecium.rutabulum6", "Bryum.capillare9", "Bryum.species9", 
"Calamagrostis.epigejos6", "Carex.arenaria6", "Carlina.vulgaris6", 
"Cerastium.semidecandrum6", "Ceratodon.purpureus9", "Cladonia.portentosa9", 
"Cladonia.ciliata9", "Cladonia.foliacea9", "Cladonia.furcata9", 
"Cladonia.scabriuscula9", "Cladonia.gracilis9", "Cladonia.grayi9", 
"Cladonia.pocillum9", "Cladonia.rangiformis9", "Conyza.canadensis6", 
"Corynephorus.canescens6", "Cynoglossum.officinale6", "Dicranum.scoparium9", 
"Diploschistes.muscorum9", "Ditrichum.flexicaule9", "Echium.vulgare6", 
"Elymus.species6", "Elytrigia.repens6", "Erodium.cicutarium.s..cicutarium6", 
"Erodium.cicutarium.s..dunense6", "Erophila.verna6", "Festuca.rubra6", 
"Fragaria.vesca6", "Galium.mollugo6", "Galium.verum6", "Geranium.molle6", 
"Helictotrichon.pubescens6", "Hieracium.pilosella6", "Hippophae.rhamnoides6", 
"Holcus.lanatus6", "Hypnum.cupressiforme9", "Hypnum.cupressiforme.v..lacunosum9", 
"Hypnum.jutlandicum9", "Hypochaeris.glabra6", "Hypochaeris.radicata6", 
"Jacobaea.vulgaris.s..vulgaris6", "Koeleria.macrantha6", "Leontodon.saxatilis6", 
"Leymus.arenarius6", "Ligustrum.vulgare6", "Linaria.vulgaris6", 
"Lotus.corniculatus6", "Luzula.campestris6", "Myosotis.ramosissima6", 
"Oenothera.species6", "Ononis.repens.s..repens6", "Phleum.arenarium6", 
"Picris.hieracioides6", "Plagiomnium.affine9", "Pleurozium.schreberi9", 
"Poa.pratensis6", "Polygala.vulgaris6", "Polytrichum.juniperinum9", 
"Polytrichum.piliferum9", "Pseudoscleropodium.purum9", "Quercus.robur6", 
"Racomitrium.canescens9", "Rosa.spinosissima6", "Rubus.caesius6", 
"Rumex.acetosella6", "Saxifraga.tridactylites6", "Senecio.inaequidens6", 
"Senecio.sylvaticus6", "Senecio.viscosus6", "Syntrichia.ruralis.v..arenicola9", 
"Syntrichia.ruralis.var..calcicola9", "Taraxacum.agaurum6", "Taraxacum.dunense6", 
"Taraxacum.rubicundum6", "Taraxacum.sectie.Erythrosperma6", "Taraxacum.species6", 
"Tulostoma.brumale9", "Veronica.arvensis6", "Veronica.officinalis6", 
"Vicia.lathyroides6", "Vicia.sativa6", "Viola.curtisii6"), NULL))

原因可能是您的数据结构。 这可能有效(假设您的数据存储在df

dt <- as.data.frame(df)
colnames(dt) <- as.character(unlist(dt[3,])) ## taking row 3 ("jaar") as column names

my_header1 <- c(rownames(dt)[1],as.character(unlist(dt[1,]))) # create a header from rowname of row 1 and the contents
my_header2 <- c(rownames(dt)[2],as.character(unlist(dt[2,]))) # same for row 2

# HTML table
kable(dt[4:nrow(dt),], format = "html", caption = "Demo Table") %>%   # picking only rows 4 ff
  kable_styling(bootstrap_options = "striped",
                full_width = F) %>%
  add_header_above(header = my_header2) %>% # adding headers
  add_header_above(header = my_header)  # adding headers

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM