fetch_csv: Read collar data stored in CSV file(s)

View source: R/fetch_csv.R

fetch_csvR Documentation

Read collar data stored in CSV file(s)

Description

Read collar data stored in CSV file(s)

Usage

fetch_csv(file_path, skip = 0, rename_fun = collar:::adj_col_nms, ...)

fetch_delim(
  file_path,
  delim = ",",
  skip = 0,
  rename_fun = collar:::adj_col_nms,
  ...
)

Arguments

file_path

The full path to one or more csv(s) to read

skip

integer, the number of rows to skip at the top of the file, see details

rename_fun

a function used to rename columns, see example

...

Other parameters to pass to readr::read_csv

delim

the delimiter to apply to the file, see ?readr::read_delim

Details

Column names are adjusted using a custom function, but the user can pass any function they want to manipulate column names (e.g. make.names). The default removes non-ASCII characters, coerces all characters to lower case and replaces "." with "_".

Value

a tibble

See Also

cllr_remove_header

Examples

#  Define file path
dir_path <- system.file(
  "extdata",
  package = "collar",
  mustWork = TRUE
)

# List files
fls <- list.files(dir_path, full.names = TRUE, pattern = "csv$")

#  Read file
ltk <- fetch_csv(fls[1])

# Read first two files
all_ltk <- fetch_csv(fls[1:2])

# You might get into trouble if too many formats are contained in the same directory,
#  this will fail because column types are different. You could specify col_types
#  argument and pass it to readr::read_csv, but because different manufacturers use
#  different column names this won't work (col_types is not vectorized).
## Not run: 
all_fls <- fetch_csv(fls)

## End(Not run)

#  Example with large header, common with companies like Telonics
#  Read in data with header and use it to find first row in next step
r1 <- fetch_csv(fls[4])

# At this point you might print the first n rows of r1
# Then find where the data start or do the following to skip lines
tlncs <- fetch_csv(fls[4], skip = which(r1[,1] == "Acquisition Time"))

# However, a function is included in this package to help with this issue
# Beware that in some cases the algorithm that finds the first row of data
#  may be overly simplistic
cllr_remove_header(r1, col_nm = `Acquisition Time`)

# Note that you can pass arguments to readr::read_*
# If reading a tab delimited file something like this might work
## Not run: 
fetch_delim(some_file_name, delim = ",\t", skip = 0, na = c("N/A"))

## End(Not run)

# The argument rename_fun can take custom functions for renaming
# Default custom function attempts to remove non-ASCII characters
colnames(fetch_csv(fls[1]))

# Simply make every name lowercase
colnames(fetch_csv(fls[1], rename_fun = tolower))

# Use the make.names function
colnames(fetch_csv(fls[1], rename_fun = make.names))




Huh/collar documentation built on Aug. 5, 2022, 11:02 p.m.