多列中变量的复杂条件替换

时间:2018-07-10 00:01:53

标签: r dataframe dplyr

我有一个数据框,其中包含一列Time和250个ID变量。每个ID变量都有三个额外的列(每个列对应某种计算)。

因此,所得数据帧具有1 + 250 x 3 + 250 = 1001列。为了使案例可重复,我包括了一个只有两个ID的数据框,其中每个变量都有1个额外的列,因此2 x 1 + 2 + 1 = 5个变量。

df <- data.frame("Time" = 1:10, 
             "ID1" = c(seq(90,95,length.out = 5), seq(120, 125, length.out = 3), seq(90, 95,length.out = 2)),
             "ID2" = c(seq(90,95,length.out = 2), seq(120, 125, length.out = 2), seq(90, 95,length.out = 6)),
             "ID1_lag_roll_mean_art" = c(0,0,0,0,0,1,1,1,0,0), 
             "ID2_lag_roll_mean_art" = c(0,0,1,1,0,0,0,0,0,0))

我正在尝试提出一种有效的方法来替换ID值,对于这些ID值,相应的“ _lag_roll_mean_art”列== 1,并且ID在“ _lag_roll_mean_art”的1:前后的平均值之间。

df_new <- data.frame("Time" = 1:10, 
             "ID1" = c(seq(90,95,length.out = 5), c(92.5, 92.5, 92.5), seq(90, 95,length.out = 2)),
             "ID2" = c(seq(90,95,length.out = 2), c(92.5, 92.5), seq(90, 95,length.out = 6)),
             "ID1_lag_roll_mean_art" = c(0,0,0,0,0,1,1,1,0,0), 
             "ID2_lag_roll_mean_art" = c(0,0,1,1,0,0,0,0,0,0))

0 个答案:

没有答案