如何基于R函数中mutate的数据帧中的另一个二进制变量创建新的二进制变量?

时间:2020-07-26 03:38:08

标签: r

数据框df具有2个变量-第一列为id,第二列为学生英语水平(lep)。 现在,我想使用规则创建一个名为sign的新列-如果lep变量的第1个1和最后一个1之间有零个,则sign = 1,否则sign = 0。任何帮助将不胜感激!

                time
index
 1            8:51 am 
 2            8:52 am
 3            8:53 am

2 个答案:

答案 0 :(得分:0)

对于每个id,您可以获取max的{​​{1}}和min索引,如果它们之间的任何值为0,则返回1。

lep = 1

答案 1 :(得分:0)

我们可以使用data.table进行此操作。将'data.frame'转换为'data.table'(setDT),按'id'分组,用lep == 1获得逻辑向量(which)的位置索引,然后从range获得的位置的最小值和最大值,获得:中的位置之间的序列(Reduce),使用该值作为索引获得相应的'lep'值,检查是否存在转换为整数(%in%并将其分配(+)到'sign'列的任意0 :=

library(data.table)
setDT(df)[, # // convert to data.table
    sign :=  # // assign to new column
         +( # // coerce logical to binary
           0 %in% # // check if 0 is there in the subset of lep
             lep[ # // extract the lep elements based on index
                Reduce(`:`, # // get the sequence 
                   range( # // returns the min and max of position index
                        which(lep == 1) # // returns the position index
                   ))]),
               by = id] # // grouped by ID
df
#         id lep sign
# 1: 3831001   1    0
# 2: 3831001   1    0
# 3: 3831001   1    0
# 4: 3831001  NA    0
# 5: 3831001  NA    0
# 6: 3831001   0    0
# 7: 3831001  NA    0
# 8: 3831001   0    0
# 9: 3831002   1    1
#10: 3831002   1    1
#11: 3831002   0    1
#12: 3831002   1    1
#13: 3831002   0    1
#14: 3831002   0    1
#15: 3831002  NA    1
#16: 3831002   1    1
# ...