在R中创建日期的正态分布。

huangapple go评论63阅读模式
英文:

Create normal distribution of dates in r

问题

以下是你要翻译的代码部分:

myvec <- seq(as.Date('2022/01/01'), as.Date('2023/01/01'), by='day')
sample(myvec, size=339, replace=TRUE, prob=c(0.2, 0.2, rep(0.1/337, 337)))
英文:

I'm trying to generate a distribution of dates from the vector "myvec," but my code isn't correctly working. Any suggest to create a code to generate normal distribution for 339 dates? Your assistance would be greatly appreciated.

myvec &lt;- seq(as.Date(&#39;2022/01/01&#39;), as.Date(&#39;2023/01/01&#39;), by=&quot;day&quot;)
sample(myvec, size = 339, replace = TRUE,
   prob = c(0.2, 0.2, rep(0.1/337, 337))
   
   )

答案1

得分: 3

如果您想要一个接近正态分布的数据,请使用 rnorm

# 围绕 2022-01-01 中心的 339 个日期,标准差为 10 天。
as.Date("2022-01-01") + round(rnorm(339, mean = 0, sd = 10))
英文:

If you want a normal-ish distribution use rnorm.

# 339 dates centered around 2022-01-01 with a sd of 10 days.
as.Date(&quot;2022-01-01&quot;) + round(rnorm(339, mean = 0, sd = 10))

huangapple
  • 本文由 发表于 2023年2月16日 02:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定