2012-03-19 57 views
0

我正在研究Latex和R,並使用以下代碼。如何避免膠乳中的指數(編號)?

<<echo=FALSE>>= 
infile<-read.table("test.txt",sep="\t") 
Col3 <- unique(infile[,3]) 
LCol3 <- length(Col3) 
for (i in 1:LCol3) { 


print(paste("Column", Col3[i])) 
print(infile[infile[,3]==Col3[i],-3]) 
} 
@ 

我得到以下輸出。

1] "Column C" 
V1 V2 V4 
1 A B D 
2 X T K 
[1] "Column Z" 
V1 V2 V4 
3 Z U M 
4 E V R 
5 Z U M 
[1] "Column P" 
V1 V2 V4 
6 E V R 

我想避免編號和列的名稱。我想要我的輸出如下。

"Column C" 
A B D 
X T K 

"Column Z" 
Z U M 
E V R 
Z U M 

"Column P" 
E V R 

我該怎麼辦?

回答

2

一般而言,print是您嘗試執行的操作的錯誤工具。您應該使用cat來代替。由於您的例子是不可複製的,這裏是表示有點類似用途很簡單的例子:對於cat

d <- data.frame(V1 = LETTERS[1:5],V2 = letters[1:5], V3 = 1:5) 

for (i in 1:5){ 
    cat("Row",i,"\n") 
    cat(unlist(d[i,]),"\n\n") 
} 

Row 1 
A a 1 

Row 2 
B b 2 

Row 3 
C c 3 

Row 4 
D d 4 

Row 5 
E e 5 

注意到,有必要不公開數據幀行,爲了能夠合理地粘貼的單個元素一起。

2

歡迎來到stackoverflow @Manish。
至於刪除編號使用latexrowname=FALSE。見下文...... DATA

Age <- c(rep('Young',2),rep('Middle',2),rep('Old',2));Age 
Smoker <- rep(c('Yes','No'),3);Smoker 
Died <- c(5,6,92,59,42,165);sum(Died) 
Lived <- c(174,213,262,261,7,28);sum(Lived) 
smoke <- data.frame(Age,Smoker,Died,Lived);smoke 

打印(xtable(煙),文件= 'ttt.tex')$結果打印在ttt.tex文件
這給

$\begin{table}[ht] 
    \begin{center} 
    \begin{tabular}{rllrr} 
     \hline 
    & Age & Smoker & Died & Lived \\ 
     \hline 
    1 & Young & Yes & 5.00 & 174.00 \\ 
     2 & Young & No & 6.00 & 213.00 \\ 
     3 & Middle & Yes & 92.00 & 262.00 \\ 
     4 & Middle & No & 59.00 & 261.00 \\ 
     5 & Old & Yes & 42.00 & 7.00 \\ 
     6 & Old & No & 165.00 & 28.00 \\ 
     \hline 
    \end{tabular} 
    \end{center}$ 

Output using print and xtable

在Hmisc使用膠乳
名稱(煙)< - NULL 膠乳(SMO柯,文件= 'ttt.tex',rowname = NULL)
我們得到

$\begin{table}[!tbp] 
\begin{center} 
\begin{tabular}{llrr} 
\hline\hline 
\multicolumn{1}{c}{Age}&\multicolumn{1}{c}{Smoker}&\multicolumn{1}{c}{Died}&\multicolumn{1}{c}{Lived}\tabularnewline 
\hline 
Young&Yes&$ 5$&$174$\tabularnewline 
Young&No&$ 6$&$213$\tabularnewline 
Middle&Yes&$ 92$&$262$\tabularnewline 
Middle&No&$ 59$&$261$\tabularnewline 
Old&Yes&$ 42$&$ 7$\tabularnewline 
Old&No&$165$&$ 28$\tabularnewline 
\hline 
\end{tabular} 
\end{center} 

,或者 Output using latex function Hmisc

我想你已經知道了,你需要在你的塊選項字段添加results=tex在裏面使用膠乳,這樣你就有一個tex輸出