Library contents

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

  1. Start with CSV row containing A,2
  2. Do this Parse the two fields
  3. Result A and 2
The comma marks the field boundary. A header can supply names for those fields.

Syntax

R 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.

From the programinput <- 'id,item,qty\n007,"bolts, steel",3'
Result007 bolts, steel 3
Lesson.R
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")
Output
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

Advertisements

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?

Given"clay, red",7
Worked resultclay, red

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?

Given"lead, stereo",2
Worked resultlead, stereo