在连续的行中替换相同的值,并在R中的值更改后停止替换 [英] Replace the same values in the consecutive rows and stop replacing once the value has changed in R

查看:41
本文介绍了在连续的行中替换相同的值,并在R中的值更改后停止替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种在每次试用开始时将连续的相同值替换为0的方法,但是一旦更改了值,它应该停止替换并保留该值.每个受试者应进行每次试验.

I want to find a way to replace consecutive same values into 0 at the beginning of each trial, but once the value has changed it should stop replacing and keep the value. It should occur every trials per subject.

例如,第一个受试者进行了多次试验(1、2等).在每次试用开始时,可​​能会有一些连续的行具有相同的值(例如1、1、1).对于这些值,我想将它们替换为0.但是,一旦值从1更改为0,我希望在其余的试验中保留这些值(例如0、0、1).

For example, first subject has multiple trials (1, 2, etc). At the beginning of each trial, there may be some consecutive rows with the same value (e.g., 1, 1, 1). For these values, I would like to replace them to 0. However, once the value has changed from 1 to 0, I want to keep the values in the rest of the trial (e.g., 0, 0, 1).

subject <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
trial <- c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2)
value <- c(1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1)
df <- data.frame(subject, trial, value)

因此,从原始数据帧开始,我想要一个新变量(value_new),如下所示.

Thus, from the original data frame, I would like to have a new variable (value_new) like below.

   subject trial value value_new
1        1     1     1         0
2        1     1     1         0
3        1     1     1         0
4        1     1     0         0
5        1     1     0         0
6        1     1     1         1
7        1     2     1         0
8        1     2     1         0
9        1     2     0         0
10       1     2     1         1
11       1     2     1         1
12       1     2     1         1

我当时正在考虑使用tidyr和group_by(主题,试验)并使用条件语句对新变量进行突变,但不知道该怎么做.我想我需要使用rle(),但是同样,我不知道如何将连续值替换为0,并且一旦值更改就停止替换,并保留其余值.

I was thinking to use tidyr and group_by(subject, trial) and mutate a new variable using conditional statement, but no idea how to do that. I guess I need to use rle(), but again, have no clue of how to replace the consecutive values into 0, and stop replacing once the value has changed and keep the rest of the values.

任何建议都将不胜感激!

Any suggestions or advice would be really appreciated!

推荐答案

您可以使用 data.table 中的 rleid :

library(data.table)
setDT(df)[, new_value := value * +(rleid(value) > 1), .(subject, trial)]
df

#    subject trial value new_value
# 1:       1     1     1         0
# 2:       1     1     1         0
# 3:       1     1     1         0
# 4:       1     1     0         0
# 5:       1     1     0         0
# 6:       1     1     1         1
# 7:       1     2     1         0
# 8:       1     2     1         0
# 9:       1     2     0         0
#10:       1     2     1         1
#11:       1     2     1         1
#12:       1     2     1         1

您也可以使用 dplyr 来完成此操作:

You can also do this with dplyr :

library(dplyr)

df %>%
  group_by(subject, trial) %>%
  mutate(new_value = value * +(rleid(value) > 1))

这篇关于在连续的行中替换相同的值,并在R中的值更改后停止替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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