将序列中的值分配给一组连续的行,而使某些行为空 [英] Assigning values in a sequence to a group of consecutive rows leaving some rows empty

查看:75
本文介绍了将序列中的值分配给一组连续的行,而使某些行为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将几个连续的行分组(并为其分配相同的值),同时使某些行保持空白(当不满足特定条件时)。

I'm trying to group several consecutives rows (and assigning them the same value) while leaving some of the rows empty (when a certain condition is not fulfilled).

我的数据是位置(xy坐标),测量它们的日期/时间以及测量之间的时间跨度。以某种方式简化后,它们看起来像这样:

My data are locations (xy coordinates), the date/time at which they were measured, and the time span between measures. Somehow simplified, they look like this:

ID   X     Y      Time    Span
1    3445  7671   0:00    -
2    3312  7677   4:00    4
3    3309  7680   12:00   8
4    3299  7681   16:00   4
5    3243  7655   20:00   4
6    3222  7612   4:00    8
7    3260  7633   0:00    4
8    3254  7641   8:00    8
9    3230  7612   0:00    16
10   3203  7656   4:00    4
11   3202  7678   8:00    4
12   3159  7609   20:00   12
...

我想为在4个小时内测量的每个位置序列分配一个值,并使我的数据看起来像这样:

I'd like to assign a value to every sequence of locations that are measured within a time span of 4 hours, and make my data look like this:

ID   X     Y      Time    Span  Sequence
1    3445  7671   0:00    -     -
2    3312  7677   4:00    4     1
3    3309  7680   12:00   8     NA
4    3299  7681   16:00   4     2
5    3243  7655   20:00   4     2
6    3222  7612   4:00    8     NA
7    3260  7633   0:00    4     3
8    3254  7641   8:00    8     NA
9    3230  7612   0:00    16    NA
10   3203  7656   4:00    4     4
11   3202  7678   8:00    4     4
12   3159  7609   20:00   12    NA

我尝试了几次带有循环 for加上 ifelse条件的算法,例如:

I've tried several algorithms with a loop "for" plus "ifelse" condition like:

Sequence <- for (i in 1:max(ID)) {
ifelse (Span <= 4, i+1, "NA")
}

没有任何运气。我知道我的尝试是不正确的,但是我的编程技能确实很基础,而且我在网络上还没有发现任何类似的问题。

without any luck. I know my attempt is incorrect, but my programming skills are really basic and I haven't found any similar problem in the web.

任何想法都将不胜感激!

Any ideas would be very appreciated!

推荐答案

下面是一个很长的衬里:

Here is a longish one liner:

ifelse(x <- DF$Span == 4, cumsum(c(head(x, 1), tail(x, -1) - head(x, -1) == 1)), NA)
# [1] NA  1 NA  2  2 NA  3 NA NA  4  4 NA

说明:


  • x 是TRUE / FALSE的向量,显示了 Span 4

  • tail(x,- 1)是写 x [2:length(x)]

  • <$的安全方法c $ c> head(x,-1)是写 x [1:(length(x)-1)] 的安全方法

  • tail(x,-1)-head(x,-1)== 1 是TRUE / FALSE的向量,显示我们从 Span!= 4 Span == 4 的位置。

  • 因为上面的向量比 x ,我在它前面加上 head(x,1) head(x,1)是写 x [1] 的安全方法。

  • 然后我将 cumsum 转换为真/假向量,将其转换为整数递增的向量:其中 Span != 4 跳到 == 4 ,它增加1,否则保持不变。

  • 所有内容都包裹在 if $code>中,因此您只会看到 x 为TRUE的数字,即,其中 Span == 4

  • x is a vector of TRUE/FALSE showing where Span is 4.
  • tail(x, -1) is a safe way of writing x[2:length(x)]
  • head(x, -1) is a safe way of writing x[1:(length(x)-1)]
  • tail(x, -1) - head(x, -1) == 1 is a vector of TRUE/FALSE showing where we went from Span != 4 to Span == 4.
  • since the vector above is one element shorter than x, I prepended head(x, 1) in front of it. head(x, 1) is a safe way of writing x[1].
  • Then I take the cumsum so it converts the vector TRUE/FALSE into a vector of increasing integers: where Span jumps from !=4 to ==4 it increases by 1, otherwise stays constant.
  • Everything is wrapped into an ifelse so you only see numbers where x is TRUE, i.e., where Span == 4.

这篇关于将序列中的值分配给一组连续的行,而使某些行为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆