Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Complete Guide to Importing Data into R - Part 2
# install
install.packages('readxl')
# library
library(readl)
# list sheets
excel_sheets('sample2.xls')
# read data from sheet 1
read_excel('sample2.xls', sheet = 1)
# read data from sheet 2
read_excel('sample2.xls', sheet = 2)
# read data from cells B3 to C7
# read data from rows 3 to 7 of columns 2 & 3
read_excel('sample2.xls', sheet = 2, range = "B3:C8")
# generate column names
read_excel('sample2.xls', sheet = 2, range = "B3:C8", col_names = FALSE)
# specify column names
read_excel('sample2.xls', sheet = 2, range = "B3:C8", col_names = c("Strain", "Res_4"))
# read data from 3 rows and 2 columns starting at the cell `A4`
read_excel('sample2.xls', sheet = 2, col_names = FALSE,
range = anchored("A4", dim = c(3, 2)))
# read all the cell from the third row of the 1st column to the 5th row of the second column
read_excel('sample2.xls', sheet = 2, range = cell_limits(c(3, 1), c(5, 2)))
# read second column
read_excel('sample2.xls', sheet = 2, range = cell_cols(2))
# read second row
read_excel('sample2.xls', sheet = 2, range = cell_rows(2))
# column type
read_excel('sample2.xls', sheet = 2, col_types = c("numeric", "text", "numeric"))
# skip first 3 rows
read_excel('sample2.xls', sheet = 2, skip = 3, col_names = FALSE)
# read 10 rows
read_excel('sample2.xls', sheet = 2, n_max = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.