Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 671745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:24:27+00:00 2026-05-14T00:24:27+00:00

I’m having a bit of a problem programming R for Sweave, and the #rstats

  • 0

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.

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:

Only 14% of respondents said ‘X’.

I want to write the report like this:

Only \Sexpr{p.mean(variable)}$\%$ of respondents said ‘X’.

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)  
}

In R, the output looks like this:

p.mean(variable)
>14

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"  

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{}?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:24:27+00:00Added an answer on May 14, 2026 at 12:24 am

    @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))}
    

    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
    

    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
    

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

    P.S.

    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. [EDIT] You will not get the weighted mean with your function if you don’t have weight function predefined, but “regular” mean!

    I hope this one helped you!

    Kind regards,
    Aleksandar

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.