Over the couple of months there have been a bunch of smaller releases to packages in the tidyverse. This includes:

This blog post summarises the most important new features, and points to the full release notes where you can learn more.

(If you’ve never heard of the tidyverse before, it’s an set of packages that are designed to work together to help you do data science. The best place to learn all about it is R for Data Science.)

forcats 0.2.0

forcats has three new functions:

x <- factor(rep(LETTERS[1:6], times = c(10, 5, 1, 1, 1, 1)))

x %>%
  fct_other(keep = c("A", "B")) %>%
  fct_count()
#> # A tibble: 3 × 2
#>        f     n
#>
#> 1      A    10
#> 2      B     5
#> 3  Other     4

x %>%
  fct_other(drop = c("A", "B")) %>%
  fct_count()
#> # A tibble: 5 × 2
#>        f     n
#>
#> 1      C     1
#> 2      D     1
#> 3      E     1
#> 4      F     1
#> 5  Other    15
x <- factor(letters[1:3])
x
#> [1] a b c
#> Levels: a b c

x %>% fct_relabel(function(x) paste0("-", x, "-"))
#> [1] -a- -b- -c-
#> Levels: -a- -b- -c-

See the full list of other changes in the release notes.

stringr 1.2.0

This release includes a change to the API: str_match_all() now returns NA if an optional group doesn’t match (previously it returned “”). This is more consistent with str_match() and other match failures.

x <- c("a=1,b=2", "c=3", "d=")

x %>% str_match("(.)=(\\d)?")
#>      [,1]  [,2] [,3]
#> [1,] "a=1" "a"  "1"
#> [2,] "c=3" "c"  "3"
#> [3,] "d="  "d"  NA
x %>% str_match_all("(.)=(\\d)?,?")
#> [[1]]
#>      [,1]   [,2] [,3]
#> [1,] "a=1," "a"  "1"
#> [2,] "b=2"  "b"  "2"
#>
#> [[2]]
#>      [,1]  [,2] [,3]
#> [1,] "c=3" "c"  "3"
#>
#> [[3]]
#>      [,1] [,2] [,3]
#> [1,] "d=" "d"  NA

There are three new features:

redact <- function(x) {
  str_dup("-", str_length(x))
}

x <- c("It cost $500", "We spent $1,200 on stickers")
x %>% str_replace_all("\\$[0-9,]+", redact)
#> [1] "It cost ----"                "We spent ------ on stickers"
fruit <- c("apple", "banana", "pear", "pinapple")

# Matching positions
str_which(fruit, "p")
#> [1] 1 3 4

# Matching values
str_subset(fruit, "p")
#> [1] "apple"    "pear"     "pinapple"

See the full list of other changes in the release notes.

readr 1.1.0

readr gains two new features:

write_csv(iris, "iris.csv.bz2")
iris2 <- read_csv("iris.csv.bz2", col_types = cols(
  Species = col_factor(levels = NULL)
))

See the full list of other changes in the release notes.

tibble 1.3.0

tibble has one handy new function: deframe() is the opposite of enframe(): it turns a two-column data frame into a named vector.

df <- tibble(x = c("a", "b", "c"), y = 1:3)
deframe(df)
#> a b c
#> 1 2 3

See the full list of other changes in the release notes.