R utils::read.csv
read.csv reads a comma-delimited table into a data frame. colClasses lets the caller retain character identifiers and parse numeric quantities separately.
A simple example
utils::read.csv
- Start with CSV row containing A,2
- Do this Parse the two fields
- Result A and 2
Syntax
read.csv(text = input, colClasses = c("character", "character", "integer"))
Run this example in R
Save the program as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.R
Preserve an identifier's leading zeros
A printed identifier has a prescribed width and may begin with zero. Verify the final text representation against the required field width before sending it to the label printer.
input <- 'id,item,qty\n007,"bolts, steel",3'
orders <- read.csv(text = input, colClasses = c("character", "character", "integer"))
cat(orders$id[1], orders$item[1], orders$qty[1], "\n")
007 bolts, steel 3
An ID such as 007 loses its leading zeros when guessed as a number. The quoted item below also demonstrates that a delimiter can be part of a field.
R documentation for utils::read.csv
Read an exhibit order
A curator enters a material and finish together inside a quoted item name. Read the quoted field as one CSV cell. What item name belongs to this row?
Read a studio purchase
A technician enters a cable description with a comma inside the field. Read the quoted field as one CSV cell. What item name belongs to this row?