两列上的子集和比较

时间:2016-06-24 05:51:44

标签: r

我有一个由三列组成的表。事件发生时的人员标识符,事件类型列(A或B)和日期列。 这显示在这里:

Person  Event   EventDate
1   A   22/03/15
1   A   22/05/15
1   B   12/12/15
1   B   12/12/15
2   B   01/13/12
2   B   02/03/12
2   B   03/08/14
2   A   05/09/14
3   B   02/02/02
3   A   03/05/14
3   B   03/06/14
3   A   17/11/15
3   A   17/02/16
3   A   18/05/16
3   A   23/06/16

我想要数据的子集。该子集应捕获eventA内的所有Person,该eventB在第一个Person Event EventDate 2 A 05/09/14 3 A 03/05/14 3 A 17/11/15 3 A 17/02/16 3 A 18/05/16 3 A 23/06/16 输出后出现:

This is the dput of the original data above
structure(list(Person..Event...EventDate = c("1   A   22/03/15", 
"1   A   22/05/15", "1   B   12/12/15", "1   B   12/12/15", "2   B   01/13/12", 
"2   B   02/03/12", "2   B   03/08/14", "2   A   05/09/14", "3   B   02/02/02", 
"3   A   03/05/14", "3   B   03/06/14", "3   A   17/11/15", "3   A   17/02/16", 
"3   A   18/05/16", "3   A   23/06/16")), .Names = "Person..Event...EventDate", class = "data.frame", row.names = c(NA, 
-15L))

我认为我遇到的问题是如何基于两列比较(Event和EventDate)来比较Person的行。

extern crate redis;

// connect to redis
fn establish_connection() -> redis::Connection {
    let client = redis::Client::open("redis://ip:port/0").unwrap();
    client.get_connection().unwrap()
}

fn main() {
    let con : redis::Connection = establish_connection();
    let _ : () = redis::cmd("RANDOMKEY").query(&con).unwrap();
}

2 个答案:

答案 0 :(得分:2)

我们可以使用module.config(["$provide", function ($provide) { $provide.decorator("$templateRequest", [ "$delegate", "$q", // DI specifications function ($delegate, $q) { // replace the delegate function $delegate = function (tpl) { var defer = $q.defer(); // convert the tpl from trustedvaluetoken to string if (typeof (tpl) !== "string" || !!$templateCache.get(tpl)) { tpl = $sce.getTrustedResourceUrl(tpl); } // make proxy call and resolve the promise; // Make an async call return defer.promise; } // return the modified delegate function return $delegate; }]); }]); 。将'data.frame'转换为'data.table'(data.table)。按'人'分组,我们setDT(df1)'人','EventDate'(转换为order课程后),然后Date'B''事件'的累积总和为1对于第一个元素,然后获取'A''Event'的行索引,并使用它来对原始数据集进行子集化。

if

或者我们可以使用library(data.table) setDT(df1)[df1[order(Person, as.Date(EventDate, '%d/%m/%y')), if(cumsum(Event == "B")[1]==1) .I[Event == "A"], by = Person]$V1] # Person Event EventDate #1: 2 A 05/09/14 #2: 3 A 03/05/14 #3: 3 A 17/11/15 #4: 3 A 17/02/16 #5: 3 A 18/05/16 #6: 3 A 23/06/16

dplyr

数据

library(dplyr)
df1 %>% 
    arrange(Person, as.Date(EventDate, '%d/%m/%y')) %>% 
    group_by(Person) %>% 
    filter(first(Event == "B") & Event == "A")
#     Person Event EventDate
#   <int> <chr>     <chr>
#1      2     A  05/09/14
#2      3     A  03/05/14
#3      3     A  17/11/15
#4      3     A  17/02/16
#5      3     A  18/05/16
#6      3     A  23/06/16

答案 1 :(得分:2)

可以使用HTTP/1.1 201 Created Location: /companies/<id> 完成此操作。我假设数据按日期排序。

sqldf