random dates in R, random numbers in R, generative art in R

How to generate random dates or numbers in R

Here is how to generate random dates or numbers in R by using base functions. There are similarities in both of the tasks, and they are useful in creating reproducible examples.

 

If you want to create a reproducible data frame that does not contains random content, look at this post.

 

Random dates in R

Here is how to generate random dates in R between two dates. In a way, it is not truly random – there are limits to randomness.

set.seed(1234)

start_date <- as.Date("2023-03-01")

end_date <- as.Date("2023-03-15")

sample(seq(start_date, end_date, by = "day"), size = 5)

#[1] "2023-03-12" "2023-03-10" "2023-03-06" "2023-03-05" "2023-03-09"

 

It is important to use the function set.seed if you want to get the same “random” result.

 

Here is how to do the same differently.

set.seed(1234)

as.Date("2023-03-01") + sample(0:14, size = 5)

#[1] "2023-03-12" "2023-03-10" "2023-03-06" "2023-03-05" "2023-03-09"

 

By default, with the function sample, you can generate results without repetition, but you can repeat values in the results with an additional argument.

set.seed(1234)

start_date <- as.Date("2023-03-01")

end_date <- as.Date("2023-03-15")

sample(seq(start_date, end_date, by = "day"), size = 5, replace = TRUE)

#[1] "2023-03-12" "2023-03-10" "2023-03-06" "2023-03-05" "2023-03-12"

 

With the function sort, you can arrange the results and see the repeating values more clearly.

set.seed(1234)

sort(sample(seq(start_date, end_date, by = "day"), size = 5, replace = TRUE))

#[1] "2023-03-05" "2023-03-06" "2023-03-10" "2023-03-12" "2023-03-12"

 

 

Random numbers in R

By using the function sample, you can generate random whole numbers in R between two values.

set.seed(1234)

sample(1:10, size = 5)

#[1] 10  6  5  4  1

 

If you like random decimal numbers, you can generate them by using the function runif.

set.seed(1234)

runif(n = 5, min = 1, max = 10)

#[1] 2.023331 6.600695 6.483473 6.610415 8.748238

 

Round the result, if you like.

set.seed(1234)

round(runif(n = 5, min = 1, max = 10), digits = 2)

#[1] 2.02 6.60 6.48 6.61 8.75

 

With the function rnorm, you can generate numbers from the normal distribution. You can specify the necessary parameters. For example, the mean of the distribution.

set.seed(1234)

rnorm(n = 5, mean = 1)

#[1] -0.2070657  1.2774292  2.0844412 -1.3456977  1.4291247

 

Here is how you can use that in scatter plot visualization.

 

A data frame with random dates and numbers in R

Here is how to generate an R data frame with random dates and numbers.

set.seed(1234)

d <- sample(seq(as.Date("2023-03-01"), as.Date("2023-03-15"), by = "day"), size = 5)
x <- sample(50:100, size = 5, replace = TRUE)
y <- sample(50:100, size = 5, replace = TRUE)

df <- data.frame("date" = sort(d), "value1" = x, "value2" = y)

df

#        date value1 value2
# 1 2023-03-05     54     88
# 2 2023-03-06     87     71
# 3 2023-03-09     65     75
# 4 2023-03-10     53     55
# 5 2023-03-12     83     64

 

Generate art in R

By the way, the featured image of this post is generated with the aRtsy package. Try it for yourself and take a look at what you can get for this day.

require(aRtsy)

set.seed(Sys.Date())

canvas_flame(colors = colorPalette("sky"))

 

Please look at other visualizations in this blog made using R. For example, gradient line chartglowing line chart, and gradient word cloud.


Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *