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 6350019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:43:52+00:00 2026-05-24T21:43:52+00:00

In a previous question , I asked whether whether a convenient wrapper exists inside

  • 0

In a previous question, I asked whether whether a convenient wrapper exists inside base R to format numbers as percentages.

This elicited three responses:

  1. Probably not.
  2. Such a wrapper would be too narrow to be useful. It is better that useRs learn how to use existing tools, such as sprintf, which can format numbers in a highly flexible way.
  3. Such a wrapper is problematic, anyway, since you lose the ability to perform calculations on the object.

Still, in my view the sprintf function is just a little bit too obfuscated for the R beginner to learn (except if they come from a C background). Perhaps a better solution is to modify format or prettyNum to have options for adding prefixes and suffixes, so you could easily create percents, currencies, degrees, etc.


Question:

How would you design a function, class or set of functions to elegantly deal with formatting numbers as percentages, currencies, degrees, etc?

  • 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-24T21:43:53+00:00Added an answer on May 24, 2026 at 9:43 pm

    I would probably keep things very simple. format() is generally useful for most basic formatting needs. I would extend that with a simple wrapper that allowed arbitrary prefix and suffix strings. Here is a simple version:

    formatVal <- function(x, prefix = "", suffix = "", sep = "", collapse = NULL,
                          ...) {
        x <- format(x, ...)
        x <- paste(prefix, x, suffix, sep = sep, collapse = collapse)
        x
    }
    

    If I were doing this for real, I would probably not have the collapse argument in the definition of formatVal(), but instead process it out of ..., but for illustration I kept the above function simple.

    Using:

    set.seed(1)
    m <- runif(5)
    

    some simple examples of usage

    > formatVal(m*100, suffix = "%")
    [1] "26.55087%" "37.21239%" "57.28534%" "90.82078%" "20.16819%"
    > formatVal(m*100, suffix = "%", digits = 2)
    [1] "27%" "37%" "57%" "91%" "20%"
    > formatVal(m*100, suffix = "%", digits = 2, nsmall = 2)
    [1] "26.55%" "37.21%" "57.29%" "90.82%" "20.17%"
    > formatVal(m, prefix = "£")
    [1] "£0.2655087" "£0.3721239" "£0.5728534" "£0.9082078" "£0.2016819"
    > formatVal(m, prefix = "£", digits = 1)
    [1] "£0.3" "£0.4" "£0.6" "£0.9" "£0.2"
    > formatVal(m, prefix = "£", digits = 1, nsmall = 2)
    [1] "£0.27" "£0.37" "£0.57" "£0.91" "£0.20"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this question is an extension to a previous question i asked (and was answered).
This stems from a previous question I asked - about a write conflict with
This is a follow up question to a previous question I asked about calculating
This is a continuation question from a previous question I have asked I now
This is a related to a previous question I have asked here, see the
This question is in continuation to my previous question, in which I asked about
This question is related to a previous question I asked, but it's a different.
this is a follow up to a previous question i asked. i am having
This is related to a previous question I asked with Jquery. If I have
In a previous question, I asked whether ORM libraries were suboptimal solutions and received

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.