Skip to main content

removing zeros from the right and adding some to the left of a number?

Suppose I have the following vector

test<- c(374500, 2270400)

First I want to get the remove the zeros, to obtain something like:

test2<- c(3745, 22704)

Then, I want to add zeros to the left in order to have 6 digits. That part I know how to do:

test3 <- formatC(test2, width = 6, format = "d", flag = "0")

Can you help me with the first step?

Solved

You can use regular expressions:

as.integer(sub("0*$", "", test))
# [1]  3745 22704

Also, here is a fun one using recursion:

remove_zeroes <- function(x) {
   x <- as.integer(x)
   i <- x %% 10L == 0
   if (any(i)) {
      x[i] <- x[i] / 10L
      Recall(x)
   } else x
}
remove_zeroes(c(123, 1230, 1230000))
# [1] 123 123 123

Benchmarks:

test <- sample.int(1e5)
library(microbenchmark)
microbenchmark(
   as.integer(sub("0*$", "", test)),
   as.integer(sub("0+$", "", test)),
   remove_zeroes(test))
# Unit: milliseconds
#                              expr       min        lq    median        uq       max neval
#  as.integer(sub("0*$", "", test)) 134.51669 138.91855 141.28812 145.96486 170.93705   100
#  as.integer(sub("0+$", "", test)) 113.91206 118.83564 123.42199 126.44162 179.03642   100
#               remove_zeroes(test)  38.01125  47.45385  49.79928  54.87592  89.05354   100

Late to the party but here's a qdap approach:

test<- c(374500, 2270400)

library(qdap)
pad(as.numeric(rm_default(test, pattern="0+$")), 6, sort=FALSE)

## [1] "003745" "022704"

Comments

Popular posts from this blog

How to make shortest code?

I use the following code to insert multi array to database: foreach($request->category as $k => $v){ $category[] = array( "category_id" => $v, "announcement_id" => $announcement->id ); } AnnouncementCategory::insert($category); So, input data is POST array $request->category . I need to refactoring this code I tried this code: $announcement->categories()->attach($request->category); In model Announcement I have: public function categories() { return $this->hasMany("App\AnnouncementCategory", "announcement_id", "id"); } Solved If you define in your Announcement model relationship like this: public function categories() { return $this->belongsToMany(AnnouncementCategory::class); } you can do it like this: $announcement->categories()->attach($request->c...

Ruby on Rails as_json limit

I have this "as_json" method in my Post model: def as_json(options={}) super(options.merge(:include => { :comments => { :include => [:user] }, :hashtags => {}, :user => {}, :group => {} })) end I'd like to set a limit attribute in :comments like this: def as_json(options={}) super(options.merge(:include => { :comments => { :include => [:user], :limit => 10 }, :hashtags => {}, :user => {}, :group => {} })) end but it doesn't work. How should I proceed? Solved I think that you have only one possibility. I suppouse that you have a has_many :comments association in your Post model So you can define the next has_many association in your Post model, something like this: has_many :ten_comments, -> { limit(10) }, class_name: "Comment", foreign_key: :post_id And then you will be able to do this in the as_json method: def as_json(options={}) super(options.merge(:include => { :te...