您可以sample()
在循环中使用该函数for
从您的姓名列表中随机抽样到数据框中:
获取一年中的所有星期一并创建名单:
dates = as.Date("2018-01-01") +7*(0:52)
namelist = c("A","B","C","D","E","F","G","H")
您尚未指定希望输出的外观,但这里有两个选择:
多列方法:
samples=matrix(0,length(dates),length(namelist))
for(i in 1:length(dates)){samples[i,]=sample(namelist)}
df=data.frame(dates,samples)
> head(df)
dates X1 X2 X3 X4 X5 X6 X7 X8
1 2018-01-01 E G B D H C A F
2 2018-01-08 F D B C G H A E
3 2018-01-15 A E H D B C G F
4 2018-01-22 E C F H A D G B
5 2018-01-29 C E F B G H A D
6 2018-02-05 C G A H D F B E
单列方法:
df2 = data.frame(dates, samples_char="")
for(i in 1:nrow(df2)){df2[i,'samples_char']=paste0(sample(namelist),collapse=", ")}
> head(df2)
dates samples_char
1 2018-01-01 E, A, G, D, C, B, F, H
2 2018-01-08 B, E, F, A, C, H, G, D
3 2018-01-15 H, D, E, C, F, A, G, B
4 2018-01-22 G, F, A, D, E, H, C, B
5 2018-01-29 A, E, D, H, G, C, B, F
6 2018-02-05 C, H, G, F, E, B, D, A