简体   繁体   English

如何考虑R中每个观察值的下一行和上一行

[英]How to consider following and previous rows of each observation in R

I need to create 2 columns: PRETARGET and TARGET based on several conditions. 我需要创建2列:基于多种条件的PRETARGETTARGET

To create PRETARGET , for each row of my data (for each participant PPT and trial TRIAL ) I need to check that the CURRENT_ID is associated with a value of 0 in the column CanBePretarget , and that the following row is the value of CURRENT_ID + 1. If these conditions are fulfil, then I would like to have a value of 0, if they are not fulfil a value of 1. 要创建PRETARGET ,对于我的数据的每一行(对于每个参与者PPT和试用版TRIAL ),我需要检查CURRENT_ID是否与CanBePretarget列中的值0 关联 ,并且下一行是CURRENT_ID + 1的值如果满足这些条件,那么如果它们不满足1的值,则我希望其值为0。

To create TARGET , for each row of my data (for each participant PPT and trial TRIAL ) I need to check that the CURRENT_ID is associated with a value of 0 in the column CanBeTarget , and that the previous row is the value of CURRENT_ID - 1. If these conditions are fulfil, then I would like to have a value of 0, if they are not fulfil a value of 1. 要创建TARGET ,对于我的数据的每一行(对于每个参与者PPT和试用版TRIAL ),我需要检查CURRENT_ID是否与CanBeTarget列中的值0 关联 ,并且上一行是CURRENT_ID -1的值如果满足这些条件,那么如果它们不满足1的值,则我希望其值为0。

In addition, if the result in PRETARGET is 1, then the value of the next row in TARGET should also be 1. 此外,如果PRETARGET中的结果为1,则TARGET中下一行的值也应为1。

I have added the desired output in the following example. 我在以下示例中添加了所需的输出。

I was thinking to use for loops and ifelse statements, but I am not sure how to consider the following/previous row of each observation. 我当时在考虑使用for循环和ifelse语句,但是我不确定如何考虑每个观察值的下一行/上一行。

PPT   TRIAL PREVIOUS_ID CURRENT_ID  NEXT_ID CURRENT_INDEX CanBePretarget CanBeTarget    PRETARGET   TARGET
ppt01 11    2           3           4       3             0              0              0           1
ppt01 11    3           4           3       4             1              0              1           0
ppt01 11    4           5           6       8             0              0              1           1
ppt01 11    6           7           8       10            0              0              1           1
ppt01 11    7           10          11      18            0              1              0           1
ppt01 11    10          11          12      19            0              0              0           0
ppt01 11    11          12          14      20            1              0              1           0
ppt01 12    1           2           1       2             1              0              1           1
ppt01 12    2           3           4       5             0              0              1           1
ppt01 12    5           6           6       8             0              0              0           1
ppt01 12    6           7           7       10            0              0              0           0
ppt01 12    7           8           9       12            0              0              0           0
ppt01 12    8           9           9       13            0              0              0           0
ppt01 12    9           10          11      16            0              0              0           0
ppt01 12    10          11          11      17            0              0              0           0
ppt01 13    1           2           2       2             1              0              1           1
ppt01 13    3           3           3       10            0              0              1           1
ppt01 13    4           5           6       13            0              0              0           1
ppt01 13    5           6           7       14            0              0              1           0
ppt01 13    9           9           10      19            0              0              0           1
ppt01 13    9           10          10      20            0              0              0           0
ppt01 13    10          11          12      22            0              0              0           0
ppt01 13    11          12          12      23            0              0              1           0
ppt01 14    10          11          11      15            0              0              0           1
ppt01 14    11          12          12      17            0              0              1           0

This can be achieved by using dplyr 这可以通过使用dplyr来实现

df.new <- df %>% 
          mutate(PRETARGET1 = abs(as.numeric(CanBePretarget == 0 & lead(CURRENT_ID, default = 0) == (CURRENT_ID + 1)) - 1)) %>% 
          group_by(PPT, TRIAL) %>% 
          mutate(TARGET1 = abs(as.numeric((CanBeTarget == 0 & lag(CURRENT_ID, default = 0) == (CURRENT_ID - 1)) ) -1),
                 TARGET1 = ifelse(lag(PRETARGET1, default = 0) == 1, 1, TARGET1))

To compare to your results, I created PRETARGET1 and TARGET1 . 为了与您的结果进行比较,我创建了PRETARGET1TARGET1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM