7.11 Other predicates
keep, discard
only keeps or discards TRUE values
iris %>%
keep(is.factor) %>%
str()
## 'data.frame': 150 obs. of 1 variable:
## $ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...some and every work like any and all
detect and detect_index finds the first element
head_while tail_while returns the head or tail while something is true
reduce applies a function until there is only one left.
Useful for repeadily joinging dataframes together
dfs <- list(
age = tibble(name = "John", age = 30),
sex = tibble(name = c("John", "Mary"), sex = c("M", "F")),
trt = tibble(name = "Mary", treatment = "A")
)
dfs
## $age
## # A tibble: 1 x 2
## name age
## <chr> <dbl>
## 1 John 30
##
## $sex
## # A tibble: 2 x 2
## name sex
## <chr> <chr>
## 1 John M
## 2 Mary F
##
## $trt
## # A tibble: 1 x 2
## name treatment
## <chr> <chr>
## 1 Mary A