如何在 R 的条件下在组内随机抽样
How to randomly sample within group with condition in R
当我尝试生成数据集时,我 运行 陷入困境。这是我的问题。
我正在使用此代码创建数据集
time0 <- rtruncnorm(100,a = 0, mean = 0.525, sd = 0.925 - 0.525)
Desc(time0)
time0.dat <- data.frame(time = "0", DV= time0)
time1 <- rnorm(100, mean = 10.65, sd = 13.025 - 10.65)
Desc(time1)
time1.dat <- data.frame(time = "1", DV= time1)
time2 <- rnorm(100, mean = 11.025, sd = 14.175 - 11.025)
Desc(time2)
time2.dat <- data.frame(time = "2", DV= time2)
time3 <- rnorm(100, mean = 5.95, sd = 8.175 - 5.95)
Desc(time3)
time3.dat <- data.frame(time = "3", DV= time3)
time4 <- rnorm(100, mean = 3.8, sd = 4.375 - 3.8)
Desc(time4)
time4.dat <- data.frame(time = "4", DV= time4)
time5 <- rtruncnorm(100,a = 0, mean = 2.075, sd = 2.75 - 2.075)
Desc(time5)
time5.dat <- data.frame(time = "5", DV= time5)
time6 <- rtruncnorm(100,a = 0, mean = 1.225, sd = 1.625 - 1.225)
Desc(time6)
time6.dat <- data.frame(time = "6", DV= time6)
time7 <- rtruncnorm(100,a = 0, mean = 0.725, sd = 1.05 - 0.725)
Desc(time7)
time7.dat <- data.frame(time = "7", DV= time7)
time8 <- rtruncnorm(100,a = 0, mean = 0.275, sd = 0.575 - 0.275)
Desc(time8)
time8.dat <- data.frame(time = "8", DV= time8)
ctdat <- rbind(time0.dat,time1.dat,time2.dat,time3.dat,time4.dat,time5.dat,time6.dat,time7.dat,time8.dat)
我想从每个“时间”中随机抽样 1,条件时间为 2 > 时间 3,时间 3 > 时间 4 ... 时间 7 > 时间 8。然后在每个采样时间,我想为其分配 ID。
所以采样后我想要的数据集应该是这样的:
谢谢。非常感谢!
这样的事情怎么样:
out <- NULL
nsamples <- 2
untime <- unique(ctdat$time)
j <- 1
while(j <= nsamples){
tmp <- NULL
for(i in seq_along(untime)){
x <- ctdat %>% filter(time == untime[i]) %>% sample_n(1)
if(untime[i] > 2){
f <- ctdat %>% filter(time == untime[i] & DV < tmp$DV[(i-1)])
if(nrow(f) == 0)break
x <- f %>% sample_n(1)
}
tmp <- rbind(tmp, x)
}
if(nrow(tmp) == length(untime)){
tmp$ID <- j
out <- rbind(out, tmp)
j <- j+1
}
}
out <- out %>% select(ID, time, DV)
# ID time DV
# 1 1 0 1.2771383
# 2 1 1 6.5078257
# 3 1 2 12.9128808
# 4 1 3 4.1991406
# 5 1 4 4.1933681
# 6 1 5 2.7821423
# 7 1 6 1.1044560
# 8 1 7 0.9538192
# 9 1 8 0.7632612
# 10 2 0 1.1390608
# 11 2 1 8.4283165
# 12 2 2 8.4993436
# 13 2 3 4.0232520
# 14 2 4 3.4199055
# 15 2 5 2.0761036
# 16 2 6 1.1407558
# 17 2 7 0.5703776
# 18 2 8 0.1484522
当我尝试生成数据集时,我 运行 陷入困境。这是我的问题。
我正在使用此代码创建数据集
time0 <- rtruncnorm(100,a = 0, mean = 0.525, sd = 0.925 - 0.525)
Desc(time0)
time0.dat <- data.frame(time = "0", DV= time0)
time1 <- rnorm(100, mean = 10.65, sd = 13.025 - 10.65)
Desc(time1)
time1.dat <- data.frame(time = "1", DV= time1)
time2 <- rnorm(100, mean = 11.025, sd = 14.175 - 11.025)
Desc(time2)
time2.dat <- data.frame(time = "2", DV= time2)
time3 <- rnorm(100, mean = 5.95, sd = 8.175 - 5.95)
Desc(time3)
time3.dat <- data.frame(time = "3", DV= time3)
time4 <- rnorm(100, mean = 3.8, sd = 4.375 - 3.8)
Desc(time4)
time4.dat <- data.frame(time = "4", DV= time4)
time5 <- rtruncnorm(100,a = 0, mean = 2.075, sd = 2.75 - 2.075)
Desc(time5)
time5.dat <- data.frame(time = "5", DV= time5)
time6 <- rtruncnorm(100,a = 0, mean = 1.225, sd = 1.625 - 1.225)
Desc(time6)
time6.dat <- data.frame(time = "6", DV= time6)
time7 <- rtruncnorm(100,a = 0, mean = 0.725, sd = 1.05 - 0.725)
Desc(time7)
time7.dat <- data.frame(time = "7", DV= time7)
time8 <- rtruncnorm(100,a = 0, mean = 0.275, sd = 0.575 - 0.275)
Desc(time8)
time8.dat <- data.frame(time = "8", DV= time8)
ctdat <- rbind(time0.dat,time1.dat,time2.dat,time3.dat,time4.dat,time5.dat,time6.dat,time7.dat,time8.dat)
我想从每个“时间”中随机抽样 1,条件时间为 2 > 时间 3,时间 3 > 时间 4 ... 时间 7 > 时间 8。然后在每个采样时间,我想为其分配 ID。
所以采样后我想要的数据集应该是这样的:
谢谢。非常感谢!
这样的事情怎么样:
out <- NULL
nsamples <- 2
untime <- unique(ctdat$time)
j <- 1
while(j <= nsamples){
tmp <- NULL
for(i in seq_along(untime)){
x <- ctdat %>% filter(time == untime[i]) %>% sample_n(1)
if(untime[i] > 2){
f <- ctdat %>% filter(time == untime[i] & DV < tmp$DV[(i-1)])
if(nrow(f) == 0)break
x <- f %>% sample_n(1)
}
tmp <- rbind(tmp, x)
}
if(nrow(tmp) == length(untime)){
tmp$ID <- j
out <- rbind(out, tmp)
j <- j+1
}
}
out <- out %>% select(ID, time, DV)
# ID time DV
# 1 1 0 1.2771383
# 2 1 1 6.5078257
# 3 1 2 12.9128808
# 4 1 3 4.1991406
# 5 1 4 4.1933681
# 6 1 5 2.7821423
# 7 1 6 1.1044560
# 8 1 7 0.9538192
# 9 1 8 0.7632612
# 10 2 0 1.1390608
# 11 2 1 8.4283165
# 12 2 2 8.4993436
# 13 2 3 4.0232520
# 14 2 4 3.4199055
# 15 2 5 2.0761036
# 16 2 6 1.1407558
# 17 2 7 0.5703776
# 18 2 8 0.1484522