表格中的值周围有不需要的引号(pgfplot)

表格中的值周围有不需要的引号(pgfplot)

我使用 R 中的 write.table 保存表,代码如下:

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
model <- z ~ x + y
results <- glm(model)
pe <- results$coefficients
vc <- vcov(results)
se <- sqrt(diag(vc))
results.table <- cbind(pe, se)
rownames(results.table) <- c("Intercept", "X-Estimate", "Y-Estimate")
write.table(results.table,file="test.csv",row.names=T,col.names=NA,sep=",")

然后,当我尝试使用 pgfplot 读取表格时,所有非数字值都用引号引起来:

% Set up
\documentclass{article}
\usepackage{pgfplotstable,filecontents,booktabs}
\pgfplotstableread[col sep=comma]{test.csv}\tablea

%Start the document
\begin{document}
% Table a

\pgfplotstabletypeset[
string type,
columns/pe/.style={column name=PE, column type = {r}},
columns/se/.style={column name=SE, column type = {r}},
every head row/.style={column type={|l}, before row={\toprule},after row=\midrule},
every last row/.style={after row={\toprule}},
]\tablea

\end{document}

我(显然)不想在所有字符串(列和行标题)周围使用双引号。我也无法使用上述代码调整列的样式,可能是因为缺少第一列标题(我想将其留空)。如果能就这两个问题提供帮助,我将不胜感激,这是一个完整的工作示例。谢谢,

答案1

按照 SlowLearner 的建议,您可以通过在 R 脚本中进行设置来删除数据表中的引号quote=F。但是,这会导致第一列的列名称为空,而 PGFPlotstable 无法处理这种情况。

我修改了分配列名的 PGFPlotstable 函数来检查列名是否为空,并用字符串替换空列名empty,这样您就可以像使用“普通”列一样使用它(例如,设置/columns/empty/.style={column name={}}为防止\pgfplotstabletypeset打印empty字符串)。

\documentclass{article}
\usepackage{pgfplotstable,filecontents,booktabs}

%% Redefine an internal PGFPlotstable macro to check for empty column names
\makeatletter
\long\def\pgfplotstableread@impl@collectcolnames@NEXT#1{%
%\pgfplots@message{Got column name no \thepgfplotstableread@curcol\ as '#1'}%
    \pgfutil@ifundefined{pgfplotstableread@impl@COLNAME@#1}{%
        \if\relax\detokenize{#1}\relax
            \def\pgfplotstable@loc@TMPa{empty}%
        \else
            \def\pgfplotstable@loc@TMPa{#1}%
        \fi
    }{% generate unique column names warning:
        \if\relax\detokenize{#1}\relax
            \def\pgfplotstable@loc@TMPa{empty}%
        \else
            \def\pgfplotstable@loc@TMPa{#1}%
        \fi
        \pgfplots@warning{Table '\pgfplotstableread@filename' has non-unique column name '\pgfplotstable@loc@TMPa'. Only the first occurence can be accessed via column names.}%
        \edef\pgfplotstable@loc@TMPa{\pgfplotstable@loc@TMPa--index\thepgfplotstableread@curcol}%
    }%
    \expandafter\def\csname pgfplotstableread@impl@COLNAME@#1\endcsname{foo}% remember this name.
    \expandafter\pgfplotslistpushbackglobal\expandafter{\pgfplotstable@loc@TMPa}\to\pgfplotstable@colnames@glob
    \ifpgfplots@tableread@to@listener
        % create an associative container colindex -> colname
        % for use in a listener.
        \expandafter\edef\csname pgfplotstblread@colindex@for@name#1\endcsname{\thepgfplotstableread@curcol}%
    \fi
    \pgfplotstableread@countadvance\pgfplotstableread@curcol
}
\makeatother

\pgfplotstableread[col sep=comma]{test.csv}\tablea

%Start the document
\begin{document}
% Table a

\pgfplotstabletypeset[
    columns/empty/.style={column name={}, string type},
    columns/pe/.style={column name=PE, fixed, fixed zerofill, precision=3,  dec sep align},
    columns/se/.style={column name=SE, fixed, fixed zerofill, precision=3,dec sep align},
    every head row/.style={before row={\toprule},after row=\midrule},
    every last row/.style={after row={\toprule}},
]\tablea

\end{document}

答案2

使用quote如下参数:

write.table(results.table,file="c:/test.csv",row.names=T,col.names=NA,sep=",", quote = FALSE)

这样应该可以去掉引号。顺便说一句,对于将来的 R 特定问题(即本问题),您可能会在 StackOverflow 上得到比在 StackExchange 的这一部分更好的答复。

更新 - 我误解了这个问题。原始发帖人想要保留write.table插入列名的“”,但他不想要引号。如果他省略引号,则不会写入“”。因此,分别写入标题和矩阵应该可以按照以下屏幕截图进行:

截屏

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
model <- z ~ x + y
results <- glm(model)
pe <- results$coefficients
vc <- vcov(results)
se <- sqrt(diag(vc))
results.table <- cbind(pe, se)
rownames(results.table) <- c("Intercept", "X-Estimate", "Y-Estimate")
## Doing it this way leads to unwanted quotes
write.table(results.table,file="c:/original.csv",row.names=T,col.names=NA,sep=",")

## Better way - first open file for reading
my.con <- file("c:/revised.csv", "w")
## Write the header, with quotes in the first column
writeLines('"",pe,se', my.con)
## Write the rest of the table as normal, without quotes
write.table(results.table, my.con, row.names = T, col.names = F, sep = ",", quote = FALSE)
close(my.con)

相关内容