Skip to contents

Using a character, or part of character select rows or columns of the data frame to either keep or remove. A more customizable way to subset your data as you can keep or remove based on partial matches, or cells containing select characters.

Usage

keep_rm(
  data,
  values,
  select,
  keep = TRUE,
  drop_levels = TRUE,
  exact = TRUE,
  colname
)

Arguments

data

A data frame.

values

A vector containing the characters or parts of characters to base selection off of.

select

If select = "row", rows containing the values will be selected for either being kept, or being removed, as specified by keep. If select = "col", columns with names containing the values will either be kept or removed, as specified by keep.

keep

If keep = TRUE the presence of the values will cause the selected rows or columns to be kept. If keep = FALSE the presence of the values will cause the selected rows or columns to be removed.

drop_levels

If drop_levels = TRUE, factor levels that have been removed will be dropped. Only applicable when select = "row"

exact

If exact = TRUE only exact matches will be selected. If exact = FALSE matches will be selected if they contain the characters in the values vector and will not be limited to exact matches only.

colname

If select = "row", colname will specify the column to select rows from.

Value

A data frame containing new selection of data.

Examples

# create data frame
Sites <- as.factor(c("One", "One", "One", "Two", "Two", "Three"))
Transect <- as.factor(c("1-Deep", "1-Shallow", "2-Shallow", "1-Shallow", "1-Deep", "1-Deep"))
Acropora.sp <- c(0.1, 0.6, 0.4, 0.9, 0.2, 0.5)
Gardineroseris.sp <- c(0.4, 0.9, 0.5, 0.23, 0.5, NA)
Psammocora.sp <- c(0.9, 0.6, 0.5, 0.8, 0.1, 0.4)
Leptastrea.sp <- c(0.5, 0.7, 0.4, 0.8, 0.2, NA)
Notes <- c(NA, NA, "saw octopus", NA, "white balance corrected", NA)
coral_cover <- data.frame(Sites, Transect, Acropora.sp, Gardineroseris.sp,
                         Psammocora.sp, Leptastrea.sp, Notes)

#Removing Notes column
keep_rm(data = coral_cover, values = c("Notes") , select = "col",
   keep = FALSE, drop_levels = FALSE, exact = TRUE)
#>   Sites  Transect Acropora.sp Gardineroseris.sp Psammocora.sp Leptastrea.sp
#> 1   One    1-Deep         0.1              0.40           0.9           0.5
#> 2   One 1-Shallow         0.6              0.90           0.6           0.7
#> 3   One 2-Shallow         0.4              0.50           0.5           0.4
#> 4   Two 1-Shallow         0.9              0.23           0.8           0.8
#> 5   Two    1-Deep         0.2              0.50           0.1           0.2
#> 6 Three    1-Deep         0.5                NA           0.4            NA

#Selecting site One and dropping extra levels
Site_One <- keep_rm(data = coral_cover, values = c("One") , select = "row",
   keep = TRUE, drop_levels = TRUE, exact = TRUE, "Sites")
levels(Site_One$Sites)
#> [1] "One"

#Removing Deep sites
Shallow_Sites <- keep_rm(data = coral_cover, values = c("-Shallow") , select ="row",
   keep = FALSE, drop_levels = TRUE, exact = FALSE, "Transect")

#Selecting only species data
Species <- keep_rm(data = coral_cover, values = c(".sp") , select ="col",
   keep = TRUE, drop_levels = TRUE, exact = FALSE)