基于阈值对ltraj对象进行子集化

时间:2017-09-17 20:12:51

标签: r

我正在使用R包adehabitatLT中的动物轨迹。 此包将动物轨迹存储为/// <summary> /// This is just some dependency that you want to use in your view models. /// </summary> interface ICustomerDatabase { void DeleteByName(string name); } class CustomerDatabase : ICustomerDatabase { public void DeleteByName(string text) { Console.WriteLine($"Customer '{text}' has been deleted."); } } /// <summary> /// This customer view model has a dependency on an /// <see cref="ICustomerDatabase"/>-Implementation, but also /// needs the customer name during construction for some reason. /// </summary> class CustomerViewModel { public CustomerViewModel(string customerName, ICustomerDatabase database) { this.Name = customerName; this.Database = database; } public string Name { get; } public ICustomerDatabase Database { get; } public void Delete() { this.Database.DeleteByName(this.Name); } } /// <summary> /// To support parameters, we use the factory pattern: The factory /// gets the view model's dependencies as its own dependencies /// by using constructor injection. The <see cref="Create"/>-Method /// then supplies any additional parameters. You can also use /// multiple factory methods that correspond with different /// constructor overloads. /// </summary> class CustomerViewModelFactory { public CustomerViewModelFactory(ICustomerDatabase service) { this.Service = service; } public ICustomerDatabase Service { get; } public CustomerViewModel Create(string text) { return new CustomerViewModel(text, this.Service); } } class Program { private static void Main() { var c = new SimpleContainer(); // First, we register the factory and the service. c.RegisterSingleton(typeof(CustomerViewModelFactory), null, typeof(CustomerViewModelFactory)); c.RegisterSingleton(typeof(ICustomerDatabase), null, typeof(CustomerDatabase)); // Later on, we depend on the factory, not the view model itself. // We would use the factory as constructor parameter // everywhere we need to create view model instances. var factory = c.GetInstance<CustomerViewModelFactory>(); // We create a view model instance using the factory. var viewModel = factory.Create("TestCustomer"); viewModel.Delete(); Console.ReadKey(); } } 个对象,并包含允许您根据某些标准将动物的轨迹划分为不同段的函数ltraj

cutltraj

其中cutltraj(ltraj, criterion) 是一个字符串,给出任何语法正确的R逻辑表达式,暗示x中的描述性参数。插图部分中的示例是:

criterion

我想分割我的动物轨迹,但我需要帮助写一个表达式来提供foo <- function(dt) { return(dt> (100*3600*24)) } newltraj <- cutltraj(bear, "foo(dt)", nextr = TRUE) 论证。 我想在连续行之间criterion的值超过阈值400时创建一个新段。

所以,我希望在以下场景中有一个新的细分: 1)myltraj$dist <400; myltraj$dist> 400, 2)myltraj$dist>&gt; 400,nrow + 1的myltraj$dist <400

如何将其作为函数编写?

1 个答案:

答案 0 :(得分:0)

我使用dplyr

中的lag()找到了解决方案
foo<-function(dist) {
    return(dist>=400 & lag(dist<400) | dist<400 & lag(dist>=400))
    }

myltraj是包含我的数据的ltraj对象的名称。

cutltraj(ltraj, "foo(dist)", nextr = TRUE))