使用上一行中的值创建具有布尔条件的计数器变量

时间:2016-07-25 03:49:00

标签: r data.table dplyr

我想根据群组 变量用户真或假 <创建计数器变量c strong>变量B 。

Redmine::OmniAuthSAML::Base.configure do |config|
  config.saml = {
    :assertion_consumer_service_url => "http://xxxxx/redmine/auth/saml/callback", # The redmine application hostname
    :issuer                         => "Redmine",                 # The issuer name
    :idp_sso_target_url             => "http://xxxxxx:8080/openam/SSORedirect/metaAlias/idp1", # SSO login endpoint
    :idp_cert_fingerprint           => "DE:xxxx", # SSO ssl certificate fingerprint
    :name_identifier_format         => "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent",
    :signout_url                    => "http://oxxxx:8080/openam/IDPSloPOST/metaAlias/idp1",
    :idp_slo_target_url             => "http://xxxxxx:8080/openam/IDPSloRedirect/metaAlias/idp1",
    :name_identifier_value          => "mail", # Which redmine field is used as name_identifier_value for SAML logout
    :attribute_mapping              => {
    # How will we map attributes from SSO to redmine attributes
      :login      => 'extra.raw_info.username',
      :firstname  => 'extra.raw_info.first_name',
      :lastname   => 'extra.raw_info.last_name',
      :mail       => 'extra.raw_info.email'
    }

变量c的期望输出

<?php
if(isset($_POST['submit'])){
  $array = array();
  while(isset($_POST['check'])){
    if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
      echo "Please include your text for each checkbox you selected.";
      exit();
    }
   $array[] = $_POST['some_text];
 }
}
?>

<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
当p为真时,

变量c是组内的计数器。 变量c 的逻辑(非代码)如下。从时间变量中可以看出,序列很重要。

DT <- data.table(time=c(1,2,3,1,1,2,3,1,1,1),user=c(1,1,1,2,3,3,3,4,4,5), B=c('t','f','t','f','f','t','t','t','t','t'))
DT

2 个答案:

答案 0 :(得分:6)

我们按'用户'分组,cumsum逻辑向量(B=="t")并将输出分配(:=)为'C'。

DT[, C:= cumsum(B=="t"), by = user]
DT
#    time user B C
# 1:    1    1 t 1
# 2:    2    1 f 1
# 3:    3    1 t 2
# 4:    1    2 f 0
# 5:    1    3 f 0
# 6:    2    3 t 1
# 7:    3    3 t 2
# 8:    1    4 t 1
# 9:    2    4 t 2
#10:    1    5 t 1

同样的逻辑可以应用于dplyr方法

library(dplyr)
DT %>%
   group_by(user) %>%
   mutate(C = cumsum(B == "t"))

答案 1 :(得分:1)

如果你使用for循环,那么以下逻辑的语法将是

String