在 R 中创建表的解释

发布于 2024-11-01 03:33:18 字数 653 浏览 0 评论 0原文

我在统计测试后从 R 数据帧在 PDF 中生成了一个乳胶表,我想创建该表的摘要,以解释其中的重要变量是什么。这怎么办?

这就是表:

  \hline
 & a & b & c & d & e \\ 
  \hline
a & 1.00 & 0.00 & 0.00 & 0.00 & 0.02 \\ 
  b & 0.00 & 1.00 & 0.00 & 0.08 & 0.40 \\ 
  c & 0.00 & 0.00 & 1.00 & 0.00 & 0.00 \\ 
  d & 0.00 & 0.08 & 0.00 & 1.00 & 0.99 \\ 
  e & 0.02 & 0.40 & 0.00 & 0.99 & 1.00 \\ 
   \hline

注意:显着变量是指值大于 0.7 的变量,例如(d 和 e 的值 =0.99)它们是显着的。我希望摘要作为表下的文本(例如,变量 d 和 e 非常相似 | a 和 e 是“弱相似”(0.02)T 与 C 更不相似),并且我想将 * 放在显着的位置数字,数字来自 R 中的数据框,

提前致谢

i have a latex table generated inside a PDF from an R data frame after a statistical test , i want to create a summary of this table that explain what are the significant variables inside it. how this can be done ??

that's the table :

  \hline
 & a & b & c & d & e \\ 
  \hline
a & 1.00 & 0.00 & 0.00 & 0.00 & 0.02 \\ 
  b & 0.00 & 1.00 & 0.00 & 0.08 & 0.40 \\ 
  c & 0.00 & 0.00 & 1.00 & 0.00 & 0.00 \\ 
  d & 0.00 & 0.08 & 0.00 & 1.00 & 0.99 \\ 
  e & 0.02 & 0.40 & 0.00 & 0.99 & 1.00 \\ 
   \hline

Note : significant varaibles are variables that have a value bigger than 0.7 , e.g (d and e have a value =0.99 ) they are signifcant. i want the summary to be as text under the table (e.g the variables d and e are significantly similar | a and e are "weakly similar" (0.02) T is even less similar with C) , and i want to put * on significant numbers , and the numbers came from a data frame in R

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-11-08 03:33:18

看看这是否符合您的要求。几乎可以肯定,有更有效的方法可以做到这一点,但没有任何有关数据结构的细节 - 这有点像读心术游戏,以了解您真正需要什么。话虽如此,假设数据来自 data.frame,这似乎可以满足您的要求:

require(xtable)
#Sample data
set.seed(1)
dat <- data.frame(matrix(abs(rnorm(25)), ncol = 5
  , dimnames = list(letters[1:5], letters[1:5])))


xtable(apply(dat, c(1,2), function(x) 
    ifelse(x > .7, paste(round(x,3), "**", sep = ""), 
    ifelse(x > .5, paste(round(x,3), "*" , sep = ""),round(x,3)))
))

产生:

% latex table generated in R 2.12.2 by xtable 1.5-6 package
% Tue Apr 12 21:23:31 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllll}
  \hline
 & a & b & c & d & e \\ 
  \hline
a & 0.626* & 0.82** & 1.512** & 0.045 & 0.919** \\ 
  b & 0.184 & 0.487 & 0.39 & 0.016 & 0.782** \\ 
  c & 0.836** & 0.738** & 0.621* & 0.944** & 0.075 \\ 
  d & 1.595** & 0.576* & 2.215** & 0.821** & 1.989** \\ 
  e & 0.33 & 0.305 & 1.125** & 0.594* & 0.62* \\ 
   \hline
\end{tabular}
\end{center}
\end{table}
> 

See if this does what you want. There are almost certainly more efficient ways to go about doing this, but without any details about the structure of your data - it is a bit of a mind reader's game to know what it is you actually need. Having said that, this seems to do what you want assuming the data comes from a data.frame:

require(xtable)
#Sample data
set.seed(1)
dat <- data.frame(matrix(abs(rnorm(25)), ncol = 5
  , dimnames = list(letters[1:5], letters[1:5])))


xtable(apply(dat, c(1,2), function(x) 
    ifelse(x > .7, paste(round(x,3), "**", sep = ""), 
    ifelse(x > .5, paste(round(x,3), "*" , sep = ""),round(x,3)))
))

Produces:

% latex table generated in R 2.12.2 by xtable 1.5-6 package
% Tue Apr 12 21:23:31 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllll}
  \hline
 & a & b & c & d & e \\ 
  \hline
a & 0.626* & 0.82** & 1.512** & 0.045 & 0.919** \\ 
  b & 0.184 & 0.487 & 0.39 & 0.016 & 0.782** \\ 
  c & 0.836** & 0.738** & 0.621* & 0.944** & 0.075 \\ 
  d & 1.595** & 0.576* & 2.215** & 0.821** & 1.989** \\ 
  e & 0.33 & 0.305 & 1.125** & 0.594* & 0.62* \\ 
   \hline
\end{tabular}
\end{center}
\end{table}
> 
只是一片海 2024-11-08 03:33:18

使用 xtable 包中的 print.xtable。然后,您可以指定可以放置所需文本的标题。然后要添加星星,您只需添加一列即可:

require(xtable)
tbl$significance <- ifelse(tbl[,5] > .7, "*", "")
print.xtable(xtable(tbl), caption = "")

Use print.xtable from the xtable package. That will then allow you to specify the caption where you can put the text you want. Then to add a star you can just add a column:

require(xtable)
tbl$significance <- ifelse(tbl[,5] > .7, "*", "")
print.xtable(xtable(tbl), caption = "")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文