other rows are data values, text or numbers, separated by commas
idea: comma is not often used within data (if it is, data value in .csv file is put in quotes)
To read this in
Fire up R Studio at r.datatools.utoronto.ca
Upload this .csv file. (Bottom right, next to New Folder, Upload.)
Click Choose File, find the file, click Open. Click OK. See file appear bottom right.
R Studio on own computer: don’t need to upload anything.
Make a new Quarto document
File, New File, Quarto Document
…and get rid of the template document (leaving the first four lines).
Make a code chunk and in it put this. Run it.
library(tidyverse)
Reading in the file
Use read_csv with the name of the file, in quotes.
Save the read-in file in something with meaningful name, here called leaning.
Make a new code chunk for this:
leaning <-read_csv("lean.csv")
Rows: 15 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): age
dbl (1): angle
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
More on the above
read_csv guesses what kind of thing is in each column. Here it correctly guesses that:
age is text (categorical variable).
angle is “double”: numbers that might have a decimal point in them.
Displaying the result
In a code chunk, put the name of your object by itself:
leaning
R Studio on your own computer
Put the .csv file in the same folder as your project. Then read it in as above like read_csv("test1.csv").
Or, use
f <-file.choose()f
which brings up a file selector (as if you were going to find a file to load or save it). Find your .csv file, the address of which will be saved in f, and then:
leaning <-read_csv(f)
When you have selected the file, comment out the file.choose line by putting a # on the front of it. That will save you having to find the file again by mistake. (Keyboard shortcut: go to the line, type control-shift-C or Mac equivalent with Cmd.)
Looking at what we read in
Again, type the name of the thing to display it:
leaning
Comments
This is a “tibble” or data frame, the standard way of storing a data set in R.
Tibbles print as much as will display on the screen. If there are more rows or columns, it will say so.
You might see navigation keys to display more rows or columns (if there are more).
View-ing your data frame
Another way to examine your data frame is to View it, like this:
View(leaning)
…or find your data frame in the Global Environment top right and click it.
This pops up a “data frame viewer” top left:
This View
Read-only: cannot edit data
Can display data satisfying conditions: click on Filter, then:
for a categorical variable, type name of category you want
for a quantitative variable, use slider to describe values you want.
Can sort a column into ascending or descending order (click little arrows next to column name).
Clicking the symbol with arrow on it left of Filter “pops out” View into separate (bigger) window.
Summarizing what we read in
It is always a good idea to look at your data after you have read the file in, to make sure you have believable numbers (and the right number of individuals and variables).
Quick check for errors: these often show up as values too high or too low, so the min and/or max will be unreasonable.
Five-number summary:
summary(leaning)
age angle
Length :15 Min. :12.00
N.unique : 2 1st Qu.:20.50
N.blank : 0 Median :28.00
Min.nchar: 3 Mean :25.87
Max.nchar: 5 3rd Qu.:32.00
Max. :34.00
Rows: 10 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): warehouse
dbl (2): size, cost
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
The data
global
Space-delimited files
Another common format for data is a text file with the values separated by spaces. Top of some other data:
This file was on my computer so I uploaded it to r.datatools.utoronto.ca first.
This time, read_delim, and we also have to say what the thing is separating the values:
coffee <-read_delim("coffee.txt", " ")
Rows: 32 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: " "
chr (1): cup
dbl (1): tempdiff
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Name of the cup, text, and tempdiff, a decimal number.
Looking at the values
coffee
These were four brands of travel mug (in cup), and for each, how much the temperature of the coffee in the mug decreased over 30 minutes.
Rows: 27 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: " "
chr (1): line
dbl (3): case, scrap, speed
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
The soap data (some)
soap
Data aligned in columns
Sometimes you see data aligned in columns, like this:
read_delim will not work: values separated by more than one space.
The number of spaces between values is not constant, because there is one fewer space before the 10.
read_table works for this.
Reading in column-aligned data
drugs <-read_table("migraine.txt")drugs
Reading an Excel sheet directly
Here is my spreadsheet from before, but tarted up a bit:
Now a workbook with a second sheet called “notes”.
Reading it in
Read into R, saying that we only want the sheet “data”. Upload spreadsheet first.
Excel spreadsheets must be “local”: cannot directly read one in from a URL.
library(readxl) # install first (once) with install.packagesleaning2 <-read_excel("lean2.xlsx", sheet ="data")
Comments
.csvfile is put in quotes)