Rmarkdown文件中的R代码:
col1 <- c("dummydata","dummydata","dummydata")
col2 <- c("dummydata","dummydata","dummydata")
col3 <- c("dummydata","dummydata","dummydata")
col4 <- c("dummydata","dummydata","dummydata")
col5 <- c("dummydata","dummydata","dummydata")
col6 <- c("dummydata","dummydata","dummydata")
col7 <- c("dummydata","dummydata","dummydata")
col8 <- c("dummydata","dummydata","dummydata")
df1 <- data.frame(col1,col2,col3,col4,col5,col6,col7,col8)
kable(df1, format="html",table.attr='class="myTable"') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
add_header_above(c("Group1" = 2, "Group2" = 2,"Group3" = 2, "Group4" = 2))
问题:我只想要组合列上的标题,即Group1,Group2等。我想删除各列上的标题,即col1,col2等。
有没有办法使用dataframe,html / javascript,Rmarkdown或任何R包来做到这一点?
答案 0 :(得分:1)
如果你经常遇到这样的问题,你应该对正则表达式和gsub函数感到满意。
这是一个解决方案,第一行是由&#34; x&lt; - &#34;
增强的最后一行代码x <- kable(df1, format="html",table.attr='class="myTable"') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
add_header_above(c("Group1" = 2, "Group2" = 2,"Group3" = 2, "Group4" = 2))
gsub("</th></tr><tr>.*</thead>","</thead>",x)
gsub的工作方式如下:查找第一个参数的匹配项,将其替换为第二个参数,并将所有这些内容添加到作为第三个参数的变量中。第一个参数中的点星(。*)表示在第三个参数中关闭之前可以跟随任何类型和数量的字符。该算法是贪婪的,它试图找到最长的匹配字符串。由于此输入参数中只有一个,这在这里工作正常。