简体   繁体   English

根据先前的行替换R中的行值

[英]Replacing row values in R based on previous rows

Below is my data frame df 下面是我的数据框df

df <- data.frame(A=c(1,1,1,1,0,0,-1,-1,-1,1,1,1,1))

I would like to have another variable T_D which maintains the first value when it encounters the change in the value of A by either 1 or -1 and replaces the next rows by 0 我想拥有另一个变量T_D ,当它遇到A值的变化为1-1时,它保持第一个值,并用0替换下一行

Expected Output: 预期产量:

A  T_D
1  1
1  0
1  0
1  0
0  0 
0  0
-1 -1
-1  0
-1  0 
1  1
1  0  
1  0
1  0   

Base R solution, this seems to work for you: Base R解决方案,这似乎适合您:

df$T_D = df$A*!c(FALSE,diff(df$A,lag=1)==0),

Find the difference between sequential rows. 查找顺序行之间的差异。 If the difference is 1, take the entry from column A, otherwise set to 0. 如果差异为1,则从A列获取条目,否则设置为0。

OUTPUT OUTPUT

    A T_D
1   1   1
2   1   0
3   1   0
4   1   0
5   0   0
6   0   0
7  -1  -1
8  -1   0
9  -1   0
10  1   1
11  1   0
12  1   0
13  1   0

dplyr 's window functions make this easy. dplyr窗口函数使此操作变得容易。 You can use the lag function to look at the previous value and see if it equals the current value. 您可以使用lag函数查看先前的值,并查看其是否等于当前值。 The first row of the table doesn't have a previous value so T_D will always be NA . 该表的第一行没有先前的值,因此T_D始终为NA Fortunately that row will always be equal to a so it's an easy problem to fix with a second mutate (or df[1,2] <- df[1,1] ). 幸运的是,该行将始终等于a因此使用第二个变量(或df[1,2] <- df[1,1] )修复是一个容易的问题。

library(tidyverse) # Loads dplyr and other useful packages

df <- tibble(a = c(1, 1, 1, 1, 0, 0, -1, -1, -1, 1, 1, 1, 1))

df %>%
  mutate(T_D = ifelse(a == lag(a), 0, a)) %>%
  mutate(T_D = ifelse(is.na(T_D), a, T_D))
df$T_D <- sign(abs(df$A)*diff(c(0, df$A)))

A data.table approach would be, 数据data.table方法是

library(data.table)
setDT(df)[, T_D := replace(A, duplicated(A), 0), by = rleid(A)][]

#     A T_D
# 1:  1   1
# 2:  1   0
# 3:  1   0
# 4:  1   0
# 5:  0   0
# 6:  0   0
# 7: -1  -1
# 8: -1   0
# 9: -1   0
#10:  1   1
#11:  1   0
#12:  1   0
#13:  1   0

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

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