first() and last()
I chose the first() and last() functions from the dyplr package.
#load the dplyr package
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(palmerpenguins)
data("penguins")
#first(x, order_by = NULL, default = NULL, na_rm = FALSE)
#x: a vector
#order_by: this is an option you can use if you want to order the vector in a certain way first before it chooses the first or last value
#default: an automatic default value is used but for more complex situations, a value should be provided
#na_rm: If you mark T, missing values will be removed. If you mark F, missing values will not be removed
#last(x, order_by = NULL, default = NULL, na_rm = FALSE)
#first() and last() functions come from the dplyr package. The first() function lets you extract the first value from a vector. The last() function lets you extract the last value from a vector.
dog_breeds <-c("Bernese Mountain Dog", "Golden Retriever", "Pug", "Poodle")
cat_breeds <- c("Maine Coon", "Siamese", "Calico", "Russian Blue")
#extracting first value from vector x
first(dog_breeds)
## [1] "Bernese Mountain Dog"
#extracting last value from vector y
last(cat_breeds)
## [1] "Russian Blue"
#These functions can be used with the summarize() function
dogandcat_df<-data.frame(x=dog_breeds,
y=cat_breeds)
summarise(dogandcat_df, first(dog_breeds), last(cat_breeds))
## first(dog_breeds) last(cat_breeds)
## 1 Bernese Mountain Dog Russian Blue
#another example with a different vector
x<-letters[1:13]
y<-letters[14:26]
#extracting first value from vector x
first(x)
## [1] "a"
#extracting last value from vector y
last(y)
## [1] "z"
Another couple of examples using the penguins dataset:
islands<-penguins$island
summarise(penguins, first(islands), last(species))
## # A tibble: 1 × 2
## `first(islands)` `last(species)`
## <fct> <fct>
## 1 Torgersen Chinstrap
#looking at flipper length with the smallest body mass within each species
penguins %>% group_by(species)%>%
summarize(flipper = first(flipper_length_mm,
order_by = body_mass_g))
## # A tibble: 3 × 2
## species flipper
## <fct> <int>
## 1 Adelie 181
## 2 Chinstrap 192
## 3 Gentoo 208
I think the first() and last() functions are just two additional ways of getting an overview of a dataset or vector. I honestly don’t think I would use them most of the time, but they aren’t unhelpful to have on hand in case you wanted to look more closely at the data.