ceiling()
and floor()
get_help()
docs
The ceiling()
and floor()
functions come with R
and are part of the Base R
{base}
package.
We use these functions to round decimals up (ceiling()
) or down (floor()
) to the closest integer.
ceiling(numeric value to round up)
floor(numeric value to round down)
ceiling(numeric array to round up)
floor(numeric array to round down)
Some examples below use the carnivores
dataset. Learn more about this dataset with get_help("carnivores")
.
# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Round the number 5.355266335 up
ceiling(5.355266335)
## [1] 6
# Round the number 5.355266335 down
floor(5.355266335)
## [1] 5
# Round the array down
<- c(101.345, 102.298, 103.887)
some_decimals floor(some_decimals)
## [1] 101 102 103
# Round up values in the `carnivores` column `awake` using dplyr::mutate()
%>%
carnivores ::mutate(awake_rounded_up = ceiling(awake)) dplyr
## # A tibble: 9 × 5
## name genus awake brainwt awake_rounded_up
## <chr> <fct> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 12
## 2 Cheetah Acinonyx 11.9 NA 12
## 3 Dog Canis 13.9 0.07 14
## 4 Gray seal Haliochoerus 17.8 0.325 18
## 5 Jaguar Panthera 13.6 0.157 14
## 6 Lion Panthera 10.5 NA 11
## 7 Northern fur seal Callorhinus 15.3 NA 16
## 8 Red fox Vulpes 14.2 0.0504 15
## 9 Tiger Panthera 8.2 NA 9