如何使用set.seed()和sample()减少用于创建可重现数据帧的代码?

时间:2019-12-23 12:27:31

标签: r

我想创建一个称为Activity的相当大且可复制的数据集,以便在StackOverFlow上提出一个问题。我的数据框将包含以下变量:

  1. DateTime:日期和时间(以毫秒为单位),数据速率为每秒11个值,即每秒11行。
  2. ID:指个人。我想创建一个数据集,其中包含3个人(ABC)的数据。
  3. x:随机数据,范围为-1至+1。
  4. y:随机数据,范围为-1至+1。
  5. z:从-1到+1的随机数据。

我最初使用此代码:

set.seed(100)
fmt <- "%Y-%m-%d %H:%M:%OS"

DateTime = seq(from=as.POSIXct("2017-08-05 14:03:55.300", format=fmt, tz="UTC"), by=1/11, length.out=67)
ID = rep("A", each=67)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
Activity1<- data.frame(DateTime,ID, x, y, z)

DateTime = seq(from=as.POSIXct("2017-08-05 16:18:12.100", format=fmt, tz="UTC"),by=1/11, length.out=67)
ID = rep("B", each=67)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
Activity2<- data.frame(DateTime,ID, x, y, z)

DateTime = seq(from=as.POSIXct("2017-08-05 20:34:31.540", format=fmt, tz="UTC"),by=1/11, length.out=67)
ID = rep("C", each=67)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)
Activity3<- data.frame(DateTime,ID, x, y, z)
Activity<- rbind(Activity1,Activity2,Activity3)

head(Activity)
                   DateTime ID     x     y     z
1 2017-08-05 14:03:55.29999  A  0.01  0.82 -0.56
2 2017-08-05 14:03:55.39090  A  0.11  0.74  0.07
3 2017-08-05 14:03:55.48182  A  0.50  0.95 -0.64
4 2017-08-05 14:03:55.57273  A  0.97 -0.89  0.95
5 2017-08-05 14:03:55.66364  A -0.97  0.78 -0.01
6 2017-08-05 14:03:55.75454  A -0.46  0.20  1.00

如何用更少的代码创建相同的数据框?我需要在StackOverFlow的另一篇文章中创建一个可重现的数据框,其他用户告诉我,我应该使用更少的代码来创建示例。

1 个答案:

答案 0 :(得分:1)

有多种方法可以达到相同的结果。这是我将使用首选工具进行的操作:

library(data.table)
# define parameters to control the process
base_data <- fread("DateTime, ID, N
2017-08-05 14:03:55.300, A, 67
2017-08-05 16:18:12.100, B, 67
2017-08-05 20:34:31.540, C, 67")[
  , DateTime := lubridate::ymd_hms(DateTime)]
# expand sequences rowwise
Activity <- base_data[, .(DateTime = seq(from = DateTime, by = 1/11, length.out = N)), 
                      by = .(rn = seq(nrow(base_data)), ID)][
                        , rn := NULL][]
# create x, y, z columns by sampling
cols <- c("x", "y", "z")
set.seed(100)
Activity[,  (cols) := replicate(length(cols), round(runif(.N, -1, +1), 2), simplify = FALSE)]

Activity
     ID            DateTime     x     y     z
  1:  A 2017-08-05 14:03:55 -0.38  0.91 -0.28
  2:  A 2017-08-05 14:03:55 -0.48  0.83 -0.12
  3:  A 2017-08-05 14:03:55  0.10  0.65  0.61
  4:  A 2017-08-05 14:03:55 -0.89 -0.36  0.04
  5:  A 2017-08-05 14:03:55 -0.06  0.76  0.39
 ---                                         
197:  C 2017-08-05 20:34:37 -0.76 -0.52 -0.81
198:  C 2017-08-05 20:34:37  0.20  0.44 -0.59
199:  C 2017-08-05 20:34:37 -0.76 -0.41 -0.94
200:  C 2017-08-05 20:34:37  0.58  0.02  0.16
201:  C 2017-08-05 20:34:37 -0.26 -0.44 -0.69

默认情况下不打印秒的分数,但可以通过以下方式验证1/11秒的分数

head(diff(Activity$DateTime))
Time differences in secs
[1] 0.09090900 0.09090924 0.09090900 0.09090900 0.09090924 0.09090900

由于操作员要求完全按照给定的种子值重现他的结果,所以我替换了

sample(seq(from = -1, to = 1, by = 0.01), size = 67, replace = TRUE)

作者

round(runif(.N, -1, +1), 2)

如果必须使用sample(),可以通过

跳过seq()部分
sample((-100:100)/100, .N, replace = TRUE)

使用data.table chaining ,代码可以更简洁地编写为

library(data.table)
cols <- c("x", "y", "z")
set.seed(100)
Activity <- fread("DateTime, ID, N
2017-08-05 14:03:55.300, A, 67
2017-08-05 16:18:12.100, B, 67
2017-08-05 20:34:31.540, C, 67")[
  , DateTime := lubridate::ymd_hms(DateTime)][
    , .(DateTime = seq(from = DateTime, by = 1/11, length.out = N)), 
    by = .(rn = seq(nrow(base_data)), ID)][
      ,  (cols) := replicate(length(cols), round(runif(.N, -1, +1), 2), simplify = FALSE)][
        , rn := NULL][]