I need some help with my R code.
I try to generate a constrained random sine function for a biological experiment. The sine refers to a daily variable temperature, divided in 24 breaks (one per hour) with the first 12 hours representing day time (category = D) and the second 12 hours representing night time (category = N). I replicated this pattern 30 times for 30 days in a month.
sine_month <- data.frame(x = rep(seq(0, 2 * pi, length.out = 24), 30),
time = rep(format(seq(from = as.POSIXct("00:00", format="%H:%M"),
by = "1 hour",
length.out = 24),
format = "%H:%M"), 30),
sin_value = rep(sin(seq(0, 2 * pi, length.out = 24)), 30),
category = rep(ifelse(sin(seq(0, 2 * pi, length.out = 24)) < 0, 'N', 'D'), 30),
day = rep(1:30, each = 24))
I added 10 to each sin_value
to get a mean of 10 degrees.
sine_10d <- sine_month %>%
mutate(sin_value = sin_value+10)
From there I need to modify the sin_value
column meeting some conditions, and I didn’t succeed.
Here are the conditions:
- For each point in
category = D
, add a random value in [0.1;3.5] - For each point in
category = N
, add a random value in [-3.5;-0.1] - Maximum
sin_value
= 13.5 - Minimum
sin_value
= 6.5 - Exactly 10
sin_value
incategory = D
= 13.5 - Exactly 10
sin_value
incategory = N
= 6.5 - The average value of each day = 10
I tried to create functions and loops but I’m stuck with the max and min condition, and with the exactly 10 values condition.
If someone know how could I get to this, please share it with me.
Regards,
Jeremy
Jérémy BACON is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.