R for Bioinformatics

Lesson 5 of 14 · 10 min

Reading CSV Data into R

A CSV file, short for comma-separated values, is a plain-text spreadsheet where each line is one row and commas separate the columns. When R reads a CSV it stores the contents in a data frame, which is R's word for a table with named columns and numbered rows. In this lesson you will load a small RNA-seq gene-count table, check that it imported correctly, and save a cleaned copy.

The working directory

The working directory is the folder on your computer where R looks for files by default. The command getwd() prints that folder, setwd() changes it to a path you supply, and anything after a # is a comment that R ignores. Point the working directory at the folder that holds your CSV so you can refer to the file by name instead of by its full path.

r
getwd()

# change to the folder that holds your data
setwd("/home/maria/rna-project")

read.csv() is the function that loads a CSV into a data frame. Its first argument is the file name in quotation marks, called a string, and header = TRUE tells R that the first row contains the column names rather than data. The arrow <- is the assignment operator, which stores the result in a variable named genes so you can reuse it later.

r
genes <- read.csv("gene_counts.csv", header = TRUE)

Always inspect a data frame right after loading it. head() prints the first six rows so you can eyeball the values, str() reports the structure and the type of every column, and summary() gives quick statistics for each column. Watch the types closely: a count column that shows chr (character text) instead of int or num means a stray non-numeric value slipped into the file.

r
head(genes)
str(genes)
summary(genes)
          gene_id gene_name control treated
1 ENSG00000141510      TP53    1204    1890
2 ENSG00000012048     BRCA1     843     402
3 ENSG00000139618     BRCA2     655     610
4 ENSG00000171862      PTEN    2130    1875
5 ENSG00000146648      EGFR     512    3400
6 ENSG00000136997       MYC    1998    2450

'data.frame':	200 obs. of  4 variables:
 $ gene_id  : chr  "ENSG00000141510" "ENSG00000012048" "ENSG00000139618" "ENSG00000171862" ...
 $ gene_name: chr  "TP53" "BRCA1" "BRCA2" "PTEN" ...
 $ control  : int  1204 843 655 2130 ...
 $ treated  : int  1890 402 610 1875 ...

   gene_id           gene_name            control          treated      
 Length:200         Length:200         Min.   :  12.0   Min.   :   8.0  
 Class :character   Class :character   1st Qu.: 340.5   1st Qu.: 298.0  
 Mode  :character   Mode  :character   Median : 720.0   Median : 655.0  
                                       Mean   : 812.4   Mean   : 903.7  
                                       3rd Qu.:1180.0   3rd Qu.:1275.0  
                                       Max.   :4820.0   Max.   :5300.0  

Once the data looks right, write.csv() saves a data frame back to a CSV file. The first argument is the data frame to write, the second is the name of the new file, and row.names = FALSE stops R from adding an extra column of row numbers. The file appears in your working directory, ready to open in Excel or share with a colleague.

r
write.csv(genes, "gene_counts_clean.csv", row.names = FALSE)

Try it yourself: Put a small CSV in your working directory, then run mydata <- read.csv("yourfile.csv", header = TRUE) and inspect it with str(mydata). Confirm that every column meant to hold numbers shows int or num rather than chr, then save a copy with write.csv(mydata, "mydata_copy.csv", row.names = FALSE) and open it in a spreadsheet to check the columns line up.