Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用样本随机选择R中的行 df id日端点 #> 1 1 1 1 #> 2 2 1 1 #> 3 3 1 1 #> 4 4 1 1 #> 5 5 2 2 #> 6 6 2 2 #> 7 7 2 2 #> 8 8 2 2 #> 9 9 3 1 #> 10 10 3 1 #> 11 11 3 1 #> 12 12 3 1_R_Dplyr - Fatal编程技术网

使用样本随机选择R中的行 df id日端点 #> 1 1 1 1 #> 2 2 1 1 #> 3 3 1 1 #> 4 4 1 1 #> 5 5 2 2 #> 6 6 2 2 #> 7 7 2 2 #> 8 8 2 2 #> 9 9 3 1 #> 10 10 3 1 #> 11 11 3 1 #> 12 12 3 1

使用样本随机选择R中的行 df id日端点 #> 1 1 1 1 #> 2 2 1 1 #> 3 3 1 1 #> 4 4 1 1 #> 5 5 2 2 #> 6 6 2 2 #> 7 7 2 2 #> 8 8 2 2 #> 9 9 3 1 #> 10 10 3 1 #> 11 11 3 1 #> 12 12 3 1,r,dplyr,R,Dplyr,在上述数据中,有一些患者(id)每天都达到终点。我试图随机选择终点患者人数s=1。对于每一天,ids在该天和以前的日子是合格的,只要以前没有选择。下面的代码得到了我期望的结果,但我必须手动输入day和endpoint值。如果您对如何直接从数据中选取这些值提出任何建议,我们将不胜感激 library(dplyr) df$s = 0 df$s <-ifelse(df$id%in%sample_n(df[df$day<=1 & df$s==0, ], 1)$id, 1, df$s

在上述数据中,有一些患者(id)每天都达到
终点。我试图随机选择
终点
患者人数
s=1
。对于每一天,
id
s在该天和以前的日子是合格的,只要以前没有选择。下面的代码得到了我期望的结果,但我必须手动输入
day
endpoint
值。如果您对如何直接从数据中选取这些值提出任何建议,我们将不胜感激

library(dplyr)
df$s = 0 
df$s <-ifelse(df$id%in%sample_n(df[df$day<=1 & df$s==0, ], 1)$id, 1, df$s) 
df$s <-ifelse(df$id%in%sample_n(df[df$day<=2 & df$s==0, ], 2)$id, 1, df$s) 
df$s <-ifelse(df$id%in%sample_n(df[df$day<=3 & df$s==0, ], 1)$id, 1, df$s) 
df
#>    id day endpoint s pick_day 
#> 1   1   1        1 0 0
#> 2   2   1        1 1 2
#> 3   3   1        1 1 1
#> 4   4   1        1 1 3
#> 5   5   2        2 1 2
#> 6   6   2        2 0 0
#> 7   7   2        2 0 0
#> 8   8   2        2 0 0
#> 9   9   3        1 0 0
#> 10 10   3        1 0 0
#> 11 11   3        1 0 0
#> 12 12   3        1 0 0
库(dplyr)
df$s=0
df$s 5 5 2 1 2
#> 6   6   2        2 0 0
#> 7   7   2        2 0 0
#> 8   8   2        2 0 0
#> 9   9   3        1 0 0
#> 10 10   3        1 0 0
#> 11 11   3        1 0 0
#> 12 12   3        1 0 0
编辑
是否可以添加一个变量来显示为其拾取行的
日期
,如上述变量
拾取日期
?谢谢

在基本R中使用
for
循环的方法:

df$s = 0 
set.seed(123)

for (i in unique(df$day)) {
   temp <- subset(df, day <= i & s == 0)
   ids <- with(temp, sample(id, endpoint[day == i][1]))
   df$s[df$id %in% ids] <- 1
}

df

#   id day endpoint s
#1   1   1        1 0
#2   2   1        1 0
#3   3   1        1 1
#4   4   1        1 1
#5   5   2        2 1
#6   6   2        2 0
#7   7   2        2 0
#8   8   2        2 1
#9   9   3        1 0
#10 10   3        1 0
#11 11   3        1 0
#12 12   3        1 0
df$s=0
种子集(123)
对于(唯一的i(df$天)){

在我的示例中,temp非常有效,在检查答案之前,我将使用实际数据对其进行测试。ThanksI通过添加
pick_day[ex_df$id%in%id]解决了这一问题