我正在用 R 编写一个包含希腊字符的表格,并且无法在 CSV 或 txt 文件中导出该表格(我想稍后在 Latex 文件中调用该表格)。
#example table:
parm1 <- 2
parm2 <- 0.3
rownames_tab <- c( paste('\u2126', "_a", sep="") , paste('\u025B',"_a", sep="") )
tab1 <- as.data.frame( matrix(ncol=1, nrow=length(rownames_tab ) ) )
row.names(tab1) <- rownames_tab
tab1[paste('\u2126', "_a", sep=""),] <- paste("Some explanation of the variable: ", parm1, sep="")
tab1[paste('\u025B', "_a", sep=""),] <- paste("Some explanation of the second variable: ", paste('\u025B', "_a", sep=""), " = " ,parm2, sep="" )
如何将表格保存在包含希腊字符(编码为UTF-8)的CSV或txt文件中?
write.csv(tab1, file="test1.csv", fileEncoding = "UTF-8")
write.table(tab1, file="test1.txt", fileEncoding = "UTF-8")
这似乎不起作用:如果您打开这些文件,它们不会读取希腊字符。
莫拉内
我找到了解决问题的方法,或者解决这个问题的方法。 因为我想在乳胶上使用表格,所以我直接用乳胶语法在 R 中编写表格:
rownames_tab <- c( "$\\omega_{a}$" , "$\\epsilon_{a}$" )
tab1 <- as.data.frame( matrix(ncol=1, nrow=length(rownames_tab ) ) )
row.names(tab1) <- rownames_tab
tab1[ "$\\omega_{a}$",] <- paste("Some explanation of the variable: ", parm1, sep="")
tab1[ "$\\epsilon_{a}$",] <- paste("Some explanation of the second variable: ", "$\\epsilon_{a}$", " = " ,parm2, sep="" )
并以乳胶可以读取的方式保存:
write.table(tab1 , "myparams.txt", quote=FALSE, eol="\\\\\n", sep=" & ", col.names = F)
我在乳胶中写道:
\begin{table*} \caption{my parameters}
\begin{tabular}{ll}
\hline
Param. & Description \\
\input{myparams.txt}
\hline
\end{tabular}
\label{tab:params}
\end{table*}
这正确显示了我的乳胶表中的希腊符号。