本文介绍了编程R/Sweave以获取正确的\ Sexpr输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为Sweave编程R时遇到了一些问题,并且#rstats twitter组经常在这里指出,所以我想我将这个问题提交给了SO人群.我是分析师,而不是程序员,所以在我的第一篇文章中要轻松一点.

I'm having a bit of a problem programming R for Sweave, and the #rstats twitter group often points here, so I thought I'd put this question to the SO crowd. I'm an analyst- not a programmer- so go easy on my first post.

这里是问题:我正在用R在Sweave中起草一份调查报告,并想使用\Sexpr{}报告边际收益.例如,与其说:

Here's the problem: I am drafting a survey report in Sweave with R and would like to report the marginal returns in line using \Sexpr{}. For example, rather than saying:

我想这样写报告:

问题是Sweave将\Sexpr{}中的表达式结果转换为字符串,这意味着R中的表达式输出和出现在我的文档中的输出是不同的.例如,在上面,我使用功能"p.mean":

The problem is that Sweave converts the results of the expression in \Sexpr{} to a character string, which means that the output from the expression in R and the output that appears in my document are different. For example, above I use the function 'p.mean':

p.mean<- function (x) {options(digits=1)
mmm<-weighted.mean(x, weight=weight, na.rm=T)
print(100*mmm)
}

在R中,输出看起来像这样:

In R, the output looks like this:

p.mean(variable)
>14

,但是当我使用\Sexpr{p.mean(variable)}时,在文档中得到的是未取整的字符串(在本例中为13.5857142857143).我试图在全局环境,函数本身以及各种命令中将函数的输出限制为digits=1.它似乎只包含R打印的内容,而不包含表达式结果的字符转换,并最终打印在LaTeX文件中.

but when I use \Sexpr{p.mean(variable)}, I get an unrounded character string (in this case: 13.5857142857143) in my document. I have tried to limit the output of my function to digits=1 in the global environment, in the function itself, and and in various commands. It only seems to contain what R prints, not the character transformation that is the result of the expression and which eventually prints in the LaTeX file.

as.character(p.mean(variable))
>[1] 14
>[1] "13.5857142857143"

有人知道我可以通过重新编程R函数或使用Sweave或\Sexpr{}中的设置来限制LaTeX文件中打印的位数吗?

Does anyone know what I can do to limit the digits printed in the LaTeX file, either by reprogramming the R function or with a setting in Sweave or \Sexpr{}?

推荐答案

@KennyTM知道了. @David,您应避免使用options(digits = 1),因为它将影响所有计算(此后将取消每个输出中的小数位).因此,在应用weighted.mean()之后使用round()函数.像这样:

@KennyTM got it. @David, you should avoid options(digits = 1) since it will affect all of your calculations (it will suppress decimals in each output afterwards). So use round() function after applying the weighted.mean(). Something like this:

\Sexpr{round(p.mean(x))}

并且请勿使用print(),但使用return().原因如下:

And do not use print(), but return(). Here's why:

> set.seed(1234)
> x <- rnorm(100)
> foo <- function(x) {
   res <- mean(x) + 5
   print(res)
}
> foo(x)
[1] 5
> foo(x) + 10
[1] 5
[1] 15

使用return()或仅在最后一行中键入结果变量:

Use return() or just type the resulting variable in the last line:

> bar <- function(x) {
   res <- mean(x) + 5
   return(res)
}
> bar(x) + 10
[1] 15

因此,重写您的函数,并确保使用as.character() ...您拥有所有的东西,现在只需将它们放在一起即可.

So, rewrite your function, and be sure to use as.character()... you have all the bits, now just put it all together.

P.S.

我不确定您的工作原理如何...我从未在分析中使用过加权均值.最让我感到困惑的是weight=weight.在函数中再添加一个参数会更好吗?坦白说,我仍然为您的函数给您正确的结果感到惊讶……这可能是因为您在函数定义之前定义了weight变量. 如果您没有预定义weight函数,则不会获得函数的加权均值,而是常规"均值!

I'm not sure how you function works... I've never used weighed mean in my analysis. The bit that's puzzling me the most is weight=weight. Wouldn't it be nicer to put one more argument in function? Frankly, I'm still amazed by the fact that your function is giving you right result... probably because you have weight variable defined prior to function definition. You will not get the weighted mean with your function if you don't have weight function predefined, but "regular" mean!

我希望这对你有帮助!

亲切的问候,亚历山达(Aleksandar)

Kind regards,Aleksandar

这篇关于编程R/Sweave以获取正确的\ Sexpr输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:38