如何根据体重复制观察结果 [英] How to replicate observations based on weight

查看:67
本文介绍了如何根据体重复制观察结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有

library(data.table)
dt <- data.table(id = 1:4, x1 = 10:13, x2=21:24, wt=c(1,0,0.5,0.7))

返回,

   id x1 x2  wt
1:  1 10 21 1.0
2:  2 11 22 0.0
3:  3 12 23 0.5
4:  4 13 24 0.7

我想在以下条件下复制观察结果:

I would like to replicate observations under the following conditions:


  1. 如果 wt 为0或1,我们分别指定 flag 等于1和0

  2. 如果0< wt <在图1中,我们分配 flag 等于0。此外,我们用 wt = 1-wt 复制此观察值,并分配 flag 等于1。

  1. If wt is 0 or 1, we assign flag equal to 1 and 0, respectively
  2. If 0 < wt < 1, we assign flag equal to 0. Further, we replicate this observation with wt = 1-wt and assign flag equal to 1.

我期望的回报是

   id x1 x2  wt flag
1:  1 10 21 1.0    0
2:  2 11 22 0.0    1
3:  3 12 23 0.5    0
4:  3 12 23 0.5    1
5:  4 13 24 0.7    0
6:  4 13 24 0.3    1

我已经尝试过使用我的代码

I have tried with my code

dt[,flag:=ifelse(wt==1,0, ifelse(wt==0, 1, 0))]
dt[,freq:=ifelse(wt > 0 & wt < 1, 2, 1)]
dtr <- dt[rep(1:.N, freq)][,Indx:=1:.N, by = id]
dtr[freq==2&Indx==2, wt:=1-wt]
dtr[Indx==2,flag:=1]
dtr[,`:=`(freq=NULL, Indx=NULL)]

但是,我认为这样效率不高。

But, I think it is not efficient.

您有任何建议吗?

推荐答案

我们可以更改一些要使其更紧凑的步骤,即删除 ifelse 并通过将逻辑转换为二进制直接使用赋值,复制行而不创建列,然后获取索引(

We can change some of the steps to make it more compact i.e. remove the ifelse and use the assignment directly by converting a logical to binary, replicate the rows without creating a column, then get the index ('i1') to assign the values in 'flag' and 'wt'.

dt1 <- dt[, flag := +(wt == 0)][rep(1:.N, (wt > 0 & wt < 1) +1)][]
i1 <- dt1[, .I[seq_len(.N)==2], id]$V1
dt1[i1, c('flag', 'wt') := .(1, 1-wt)][]
#    id x1 x2  wt flag
#1:  1 10 21 1.0    0
#2:  2 11 22 0.0    1
#3:  3 12 23 0.5    0
#4:  3 12 23 0.5    1
#5:  4 13 24 0.7    0
#6:  4 13 24 0.3    1

这篇关于如何根据体重复制观察结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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