我想向此数据集添加一个额外的列。如果每个Household有两个以上的ID,我希望将具有Main==0的Household成员的Variable值相加并作为新变量添加到具有Main==1的Household成员的行中,反之亦然。我的开始如下所示: # create a var with the nr of hh members
DTattempt <- setDT(DT)[, count:= .N, by=Household]# if there is only 1 hh member, the value of New_Var =
我有一个数据框架: id groupx1 Bx2 A
x3 B 我想创建一个新的列new_group,条件如下:如果在同一个id中有两个唯一的group值,比如第1行和第2行中的A组和B组,那么new_group的值应该是"two“。如果同一个id 中只有1个唯一的group值,则new_group的值应为该组A。否则,请指定B