我正在用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")
这似乎不起作用:如果您打开这些文件,它们不会读取希腊字符。
非常感谢你的帮助,
最好的
莫兰
发布于 2018-04-26 22:36:25
我找到了解决问题的方法,或者说找到了绕过这个问题的方法。因为我想在latex上使用表,所以我直接用latex语法在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="" )
并以latex可以读取的方式保存它:
write.table(tab1 , "myparams.txt", quote=FALSE, eol="\\\\\n", sep=" & ", col.names = F)
在latex中我写道:
\begin{table*} \caption{my parameters}
\begin{tabular}{ll}
\hline
Param. & Description \\
\input{myparams.txt}
\hline
\end{tabular}
\label{tab:params}
\end{table*}
这正确地显示了我的latex表中的希腊符号。
https://stackoverflow.com/questions/49983988
复制相似问题