R:在子集/选择函数中选择所有值

时间:2014-08-25 11:57:31

标签: r select

我正在尝试创建一个交互式功能,其中可以从列表中选择多个值,传递给根据提供的信息选择的功能。使用

subset(..., select(..., subset(...))) 

非常适合选择特定值,只需在其中一个选择行前放置一个“#”就可以提取所有值。代码的前置函数版本低于

我想以

的形式提供参数
Service = c("SMS", "Data", ...), Country = c("Cyprus", "Egypt") 

对于每个选择,我希望能够为那些没有选择的参数选择所有值。这在SQL中很容易,

SELECT *

但我在R Select()函数中找不到任何等效术语。

# Selection
dashselect <- subset(DashboardSource, select=c(Date, Currency,Direction,Partner,
                                                   Agreement, Country, Service,     Amount..USD.,
                                           Minutes, Mb, SMS, DbSMonth, DbSYear),
                 subset=c(
                           (Direction == "Outbound"  ) &
#                               (Country=="Portugal" | Country=="Egypt") &
                           (Service =="SMS" | Service == "Data") &
#                               (Date >= "2013/1/1" & Date <= "2015/01/01" ) &
#                               (Partner =="") &
#                               (Agreement == "")  
#                               (Minutes > "")
                           (Mb >1000) &
#                               (Amount..USD. > "") 
                          (SMS >= 0)
                          )
                )

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

对于每个输入参数,请执行以下操作:

<%= form_with(model: building, local: true) do |form| %>
  <% if building.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(building.errors.count, "error") %> prohibited this building 
       from being saved:</h2>

     <ul>
       <% building.errors.full_messages.each do |message| %>
        <li><%= message %></li>
        <% end %>
       </ul>
      </div>
      <% end %>

      <div class="field">
         <%= form.label :name %>
          <%= form.text_field :name %>
      </div>

      <div class="field">
        <label> How many floors does the building have</label>
        <input type="number" name="number"/>
      </div>

      <div class="actions">
        <%= form.submit %>
      </div>
   <% end %>

现在使用以下方法查询您的数据框:

selDate <- DashboadSource$Date %in% select$Date# Logical index will be all FALSE if select$Date is NULL (i.e. no argument provided)
if(all( selDate == FALSE)){ selProj <- selProj == FALSE}# If all FALSE, change to all TRUE

编辑: 作为改进的实现,您可以首先定义此功能:

dashSelect <- DashboadSource[selDate & selCurrency & ... & selDbSYear, ]

然后依次在每个输入参数上使用该函数:

nullProof <- function(selIn){
if(all(selIn == F)){selOut <- selIn == F}
else{selOut <- selIn}
return(selOut) }

然后如上所述查询您的数据框。