2010-03-04 32 views
2

我有一些數據(ddply函數的輸出),我想在xtable中出現以供其他地方使用。你可以在R表中合併單元格R

calqc_table<-structure(list(RUNID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), ANALYTEINDEX = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), ID = structure(1:11, .Label = c("Cal A", "Cal B", "Cal C", 
"Cal D", "Cal E", "Cal F", "Cal G", "Cal H", "Cal High", "Cal Low", 
"Cal Mid"), class = "factor"), mean_conc = c(200.619459644855, 
158.264703128903, 102.469121407733, 50.3551544728544, 9.88296440865076, 
4.41727762501703, 2.53494715706024, 1.00602831741361, 199.065054555735, 
2.48063347296935, 50.1499780776199), sd_conc = c(2.3275711264554, 
NA, NA, NA, NA, NA, NA, 0.101636943231162, 0, 0, 0), nrow = c(3, 
1, 1, 1, 1, 1, 1, 3, 2, 2, 2)), .Names = c("RUNID", "ANALYTEINDEX", 
"ID", "mean_conc", "sd_conc", "nrow"), row.names = c(NA, -11L 
), class = "data.frame") 
calqc_xtable<-xtable(calqc_table) 
print(calqc_xtable,type="html") 

它給了我html格式的表。但是,我想將RUNID和ANALYTEINDEX列的內容垂直地合併到值相同的位置。任何人都知道如何通過xtable(或其他方式)來做到這一點?

回答

3

我會「清理」原始數據幀,用NA代替等於先前值的值。 xtable會將這些缺失值呈現爲空白。如果你沒有水平邊框線,這看起來會很好。

cleanf <- function(x){ 
    oldx <- c(FALSE, x[-1]==x[-length(x)]) # is the value equal to the previous? 
    res <- x 
    res[oldx] <- NA   
    res} 

現在我們可以應用此功能,你想清理列:

clean.cols <- c("RUNID", "ANALYTEINDEX") 
calqc_table[clean.cols] <- lapply(calqc_table[clean.cols], cleanf) 
+0

是的,這幾乎是完美的(使用一個向量化功能額外的榮譽,我會已經打破了明年循環肯定...) – PaulHurleyuk