将矢量元素除以前面的连续0的数量

时间:2017-08-06 20:30:52

标签: r

我有这些数据:

                  <%= form_for(@referral_form, url: submit_referral_form_url, method: :post, :remote => true ) do |f| %>
                <div class="contact-us-error"><ul></ul></div>
                <div class="form-group has-success has-feedback">
                  <%= f.text_field :name, :class=>"form-control", :placeholder => "Adınız & Soyadınız", required: true %>
                  <span class="glyphicon glyphicon-user form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-success has-feedback">
                  <%= f.text_field :phone, :class=>"form-control", :placeholder => "Telefon Numaranız", required: true %>
                  <span class="glyphicon glyphicon-phone form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-success has-feedback">
                  <%= f.text_field :friend_name, :class=>"form-control", :placeholder => "Arkadaşınızın Adı & Soyadı", required: true %>
                  <span class="glyphicon glyphicon-user form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-success has-feedback">
                  <%= f.text_field :friend_phone, :class=>"form-control", :placeholder => "Arkadaşınızın Telefon Numarası", required: true %>
                  <span class="glyphicon glyphicon-phone form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-success has-feedback">
                  <%= f.email_field :email, :class=>"form-control" ,  type:'email', required: true, placeholder: "Email Adresiniz" %>
                  <span class="glyphicon glyphicon-envelope form-control-feedback" aria-hidden="true"></span>
                </div>
                <div class="form-group has-success has-feedback">
                  <%= f.text_area :message, :class=>"form-control", :placeholder=>"Arkadaşınızın Arabasının Özelliği (Yıl, Marka, Model vb)", required: true %>
                </div>
                <div class="form-group has-success has-feedback">
                  <%= f.hidden_field :campaign_name, :value => "Nusret-2017"%>
                </div>
                <div class="form-group has-success has-feedback">
                <%= f.submit "Gönder", class: "btn createaccbtn" %>
                </div>
                <% end %>

我希望使用R:

获得以下输出
20 0 20 40 0 0 0 40 20 0 20 0 20 0 0 60

2 个答案:

答案 0 :(得分:1)

据我所知,这是您正在寻找的算法:

# data:
# original vector
a <- c(20, 0, 20, 40, 0, 0, 0, 40, 20, 0, 20, 0, 20, 0, 0, 60)
# expected results
b <- c(20, 10, 10, 40, 10, 10, 10, 10, 20, 10, 10, 10, 10, 20, 20, 20)

ramon_algo <- function(vec) {
  # Ramon algorithm?
  # Does something special when it encounters a 0 in a vector...

  n <- length(vec)
  result <- numeric(n)
  index <- 1L
  zeros_inrow <- 0L
  while (index <= n) {
    if (vec[index] != 0) {
      result[(index - zeros_inrow):index] <- vec[index] / (zeros_inrow + 1)
      zeros_inrow <- 0L
      index <- index + 1L
    } else {
      zeros_inrow <- zeros_inrow + 1L
      index <- index + 1L
    }
  }
  result
}

# Now testing it:
ramon_algo(a)
 [1] 20 10 10 40 10 10 10 10 20 10 10 10 10 20 20 20

identical(ramon_algo(a), b)
[1] TRUE

这可能不是最快的方式,但至少是一个开始。

答案 1 :(得分:1)

基本上,这可以通过在每个组内进行适当的分组和平均来实现。在data.table的帮助下,这变成了一个&#34;单行&#34;:

library(data.table)
data.table(a)[order(rev(seq_along(a))), b := mean(a), by = cumsum(a != 0)][, b]
 [1] 20 10 10 40 10 10 10 10 20 10 10 10 10 20 20 20

解释

将一个向量的元素除以前面之前的连续0的数量,用所有数字的平均值替换零和后续的非零数字。因此可以减少问题以定义组。

更容易识别非零数字,后跟一系列零,而不是找到一组非零数字后面的零。所以,如果我们颠倒a我们得到的顺序

rev(a)
 [1] 60  0  0 20  0 20  0 20 40  0  0  0 40 20  0 20

使用

cumsum(rev(a) != 0)
 [1] 1 1 1 2 2 3 3 4 5 5 5 5 6 7 7 8

我们得到反向向量中每个非零项的连续计数。这可以用作非零及其后续零的组索引,用于平均该组。使用data.table是因为其语法简洁,可以使用按引用分配 :=将组平均值分配给组的所有行。