我有一列日期时间变量ex
,我想从中随机采样一个元素并将其分配给向量。但是,如果这样做,我不会得到结果:
> sample(ex,1)
[1] "2017-12-24 10:42:39 GMT"
> some_vector<-rep(NA,1)
> some_vector[1]<-sample(ex,1)
Warning message:
In some_vector[1] <- sample(ex, 1) :
number of items to replace is not a multiple of replacement length
> some_vector
[[1]]
[1] 20
示例POSIXlt
ex
structure(list(sec = c(39, 31, 12, 20), min = c(42L, 44L, 13L,
22L), hour = c(10L, 10L, 15L, 15L), mday = c(24L, 24L, 14L, 14L
), mon = c(11L, 11L, 0L, 0L), year = c(117L, 117L, 118L, 118L
), wday = c(0L, 0L, 0L, 0L), yday = c(357L, 357L, 13L, 13L),
isdst = c(0L, 0L, 0L, 0L), zone = c("GMT", "GMT", "GMT",
"GMT"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon",
"year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt",
"POSIXt"))
我想将some_vector
输出为
some_vector
[1] "2017-12-24 10:42:39 GMT"