How to display the minimum temperature sequence using rle() in R?


This Content is from Stack Overflow. Question asked by Shawn Frost

The first task is to set the random number generator seed to 100.


Generate 100 random values in a normal distribution with m=7 & sd=4.

temp <- c(rnorm(100, mean = 7, sd  = 4))

Set names for each value in the data set.

names(temp) <- paste0("D-", 1:100)

round each value to 1 decimal place.

temp <- round(temp, digits = 1)

1.Calculate and display the number of days where the temperature was greater than the mean.

 gt_mean <- mean(temp)
    gt_temp <- temp[temp > gt_mean]

2.Display the day with the maximum temperature, using cat().

 max_temp <- max(temp)
    n = names(temp)[match(max_temp,temp)]
    cat("The max temp was on day", n, "with a value of", max_temp)

3.Display the day with the minimum temperature.

 min_temp <- min(temp)
    n = names(temp)[match(min_temp,temp)]
    cat("The min temp was on day", n, "with a value of", min_temp)

4.Create a parallel vector called warnings, which has two values Warning or Normal,
where a temperature weather warning is in place if the temperature is less than or equal to

 warnings <- c(ifelse(temp <= 4.0, 'Warning','Normal'))
  1. Display the number of days where the weather warning was in operation.

    warn_days <- c(warnings == “Warning”)
    disp <- temp[warn_days]
    disp_days = length(disp)
    cat(“The number of days the warnings were in operation =”,disp_days)

  2. Display the days where the weather warning was in operation.

    ww = names(temp)[match(disp,temp)]

  3. Display the warning in a tabular format.

    tw <- table(warnings)

  4. Use the function rle() to and out the maximum sequence of weather warnings in the

    len <- rle(warnings == “Warning”)
    tw <- table(len)

     len1 <- max(len$lengths)


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?