Day 1 — Kinds of files: plain text, Word and Excel
Lecture slides · Quiz · Weekly revision
Which kind of file holds the measurements?
Did seedlings grow differently after a nutrient treatment? The measurements are simulated teaching data from 24 seedlings in separate pots, 12 treated and 12 controls, each measured at the start and after seven days; data/README.md describes the study. Before we analyse them we look at the file they are stored in. Some files are plain text, which any program can read. Others, such as Word and Excel files, are packages that only particular programs understand, and spreadsheets can even change the data. No programming today.
Downloading the course folder
- Download the course files.
- In the browser’s downloads list, choose Show in folder (Windows) or Show in Finder (Mac). The file is usually in Downloads.
- Extract the ZIP: on Windows, right-click
course-files.zipand choose Extract All; on Mac, double-click it. - Move the extracted folder
introcomputing-courseinto Documents and work there, never inside the ZIP. It contains the foldersdata,examples,outputandreports, and the filesreport.Rmdandintrocomputing.Rproj.
Extraction can create an extra outer folder, and downloading twice gives names ending in (1). Open folders until you find the one that directly contains data, output and introcomputing.Rproj, and use that one. Ask before deleting the other.
Tell the teacher. A cloud-managed Documents folder can show a file (with a cloud icon) that is not yet on the laptop, so we check that the course files open with the network turned off.
The ending of a filename, such as .csv or .docx, is its extension. File managers often hide it, so turn it on: in Windows File Explorer choose View → Show → File name extensions; in Mac Finder choose Finder → Settings → Advanced → Show all filename extensions.
A plain-text editor
A text editor shows exactly the characters stored in a file, with no formatting. On Windows, use Notepad: right-click a file and choose Open with → Notepad. On Mac, use TextEdit, set to plain text under TextEdit → Settings → New Document → Plain text. TextEdit tries to display Word files as documents, so to see what is really inside a file use File → Open, click Options and tick Ignore rich text commands. Later in the course, RStudio will be our text editor.
What is inside seedlings.csv?
Open data/seedlings.csv in the text editor to find out how much plant P01 grew. The first lines are:
plant_id,treatment,initial_mm,final_mm
P01,control,25.7,44.3
P02,control,24.3,32.3
The first line is a header that names the columns, and each following line describes one plant. P01 started at 25.7 mm and ended at 44.3 mm, so it grew 44.3 − 25.7 = 18.6 mm. Nothing else is in the file: only letters, digits, commas and line breaks. Such a file is plain text, and a CSV (“comma-separated values”) file is a table written as plain text. Every editor shows the same characters, and R can read them directly.
Plain text works for anything that can be written as characters. Open data/dna.txt and you see a DNA sequence of the letters A, C, G and T. The plain-text files you will meet in this course are:
| Extension | What it holds | Example |
|---|---|---|
.csv, .tsv |
tables (commas or tabs between values) | data/seedlings.csv |
.txt |
notes, DNA sequences | data/dna.txt, data/field-notes.txt |
.md |
documentation, with simple marks such as # for headings |
data/README.md |
.R, .Rmd |
R code, and R Markdown reports | report.Rmd |
What is inside a Word file?
The folder examples holds the same short note about the experiment as seedling-notes.txt and as seedling-notes.docx, saved by a word processor. Open both in the text editor (not in Word) to find out where the Word file keeps the text. The .txt shows the note; the .docx starts with PK followed by unreadable symbols, because a .docx file is a compressed ZIP package.
To look inside, copy examples/seedling-notes.docx into output, rename the copy seedling-notes.zip (accept the warning) and extract it. Inside the folder word, open document.xml in the text editor. The sentences of the note are there, surrounded by tags such as <w:p> and <w:t> that mark paragraphs and text. The package holds 16 files, among them styles.xml with fonts and headings and docProps/core.xml with the author and dates, and Word puts them together when it opens the document.
PK
Use File → Open → Options → Ignore rich text commands. If that does not help, copy the .docx into output, rename the copy to end in .txt, and open the copy.
Excel files (.xlsx) are packages of the same kind. PDF files and pictures are not text either: open examples/seedling-notes.pdf and examples/seedlings-plot.png in the text editor, and the first characters, %PDF and PNG, tell programs which format follows.
The same table as CSV and as Excel
Open examples/seedlings.xlsx in a spreadsheet program (Excel or LibreOffice Calc) to find out what an Excel file stores beyond the measurements. It holds the same 24 plants as data/seedlings.csv, plus a bold, coloured header row, a column growth_mm whose cells are formulas (click E2 and you see =D2-C2 while the cell shows 18.6), and a second sheet called notes.
A CSV file can store none of these: it holds values only, one table per file. That is why this course keeps data as CSV. Any program can read it, including R and a text editor, and what the text editor shows is exactly what is stored. Formatting and calculations belong in R code, where every step is written down; a coloured cell is information that R never sees.
Can a spreadsheet change the data?
Open examples/genes.csv in the text editor and then in Excel to find out what Excel does to the gene symbols SEPT2 and MARCH1. Excel guesses that they are dates and shows 2-Sep and 1-Mar. If the file is saved, the gene names are replaced for good. (LibreOffice usually keeps the names; the lecture slides show what Excel does.) Ziemann and colleagues (2016) found such errors in about one in five published papers that had gene lists as Excel files, and in 2020 the human gene naming committee (HGNC) renamed these genes, for example to SEPTIN2 and MARCHF1, partly for this reason. Older data files still use the old names.
Then open examples/seedlings-semicolon.csv in the text editor and compare it with data/seedlings.csv:
plant_id;treatment;initial_mm;final_mm
P01;control;25,7;44,3
This is how Excel saves “CSV” on a computer set to Swedish (and many other European languages): the comma is the decimal sign, so semicolons separate the values. A program that expects commas splits these rows in the wrong places, so look at every CSV in a text editor before analysing it.
For this course: if you look at raw data in a spreadsheet, close it without saving.
Text editor or word processor?
A word processor (Word, Pages, Google Docs) is made for formatted documents and changes what you type. Compare the last line of the two notes from examples to find out what Word did to a line of R code:
seedling-notes.txt: seedlings <- read.csv("data/seedlings.csv")
seedling-notes.docx: seedlings <- read.csv(“data/seedlings.csv”)
Word replaced the straight quotes " with curly quotes “ ”. R understands only straight quotes, so code copied from the Word file stops with Error: unexpected input. Write code, data and notes for analysis in a text editor, and later in RStudio. TextEdit on Mac is a word processor too unless you choose Format → Make Plain Text before saving; otherwise it saves a formatted .rtf file.
Renaming and file extensions
Renaming a file changes its name, not its contents. Renaming the .docx to .zip worked because the file was a ZIP package all along. Renaming seedlings.csv to seedlings.xlsx does not make an Excel file, and Excel refuses to open it because the contents are still plain text. Changing a format needs a program that rewrites the file, such as Save As with another format.
A practice copy of the measurements
The files in data stay exactly as supplied, so that you can always start again from what was measured. Copy data/seedlings.csv into output and rename the copy seedlings-practice.csv, checking that the name still ends in .csv. Open the copy in a spreadsheet program to see the same 24 plants as a table, then close it without saving. Tomorrow’s exercises use this copy.
Your first plain-text note
Create a new plain-text file in the text editor (on Mac, Format → Make Plain Text) and write a few sentences: what one row of data/seedlings.csv describes and in which units, one thing an .xlsx file can store that a .csv file cannot, and why raw data should not be saved from a spreadsheet. Use Save As to save it as day01-notes.txt in reports, and check in the file manager that the name ends in .txt and not .txt.txt or .rtf. Then close the editor, go to reports in the file manager and open the note again to check that your last sentence was saved.
Which files in the course folder are plain text?
Open each file in the text editor to find out which files in the course folder R could read directly. Check every file in data and in examples, and report.Rmd, introcomputing.Rproj and START-HERE.txt in the project folder, without changing or saving any of them. Write the result in a new plain-text file reports/file-types.txt, one line per file, with the kind of program meant to open each file that is not plain text:
data/seedlings.csv plain text table, commas between values
examples/seedlings.xlsx not text spreadsheet program (ZIP package)
If a file shows only readable words and numbers, it is plain text; if it starts with PK, %PDF or PNG and then unreadable symbols, it is not.
Plain text: every file in data, report.Rmd, introcomputing.Rproj (a few RStudio settings written as text), START-HERE.txt, and in examples the files README.txt, seedling-notes.txt, genes.csv and seedlings-semicolon.csv. Not plain text: seedling-notes.docx (word processor), seedlings.xlsx (spreadsheet), seedling-notes.pdf (PDF reader) and seedlings-plot.png (image viewer).
Check your understanding
.docx file in Notepad and see PK and strange symbols. Is the file broken?
No. A .docx is a compressed ZIP package of XML files, so Notepad shows its raw bytes, while Word unpacks the package and shows the formatted document.
.xlsx and MARCH1 appears as 1-Mar. What happened?
A spreadsheet converted the gene symbol to a date when the list was opened and saved, and the name cannot be recovered from the date. Ask for the original file.
seedlings-practice.csv to seedlings-practice.txt. What changed?
Only the name. The file still holds the same commas and numbers, and a text editor shows the same contents.
Save today: reports/day01-notes.txt, reports/file-types.txt and output/seedlings-practice.csv.
Next: folders and paths.