Importing data into R Flashcards

1
Q

function to load in CSV files

A

read.csv(“data.csv”, stringsAsFactors = FALSE)
data must be in your working directory, or the path must be specified. Strings as factors default is TRUE, sets strings as factors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List the files in your working directory

A

dir()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

import tab delimited data

A

read.delim(x, sep = “/t” (space), header = TRUE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

import any tabular data

A

read.table(x, sep = “”, header = FALSE, stringsAsFactors = TRUE, col.names = “”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

which.min

A

returns the index of the smallest value in a vector

ex: cars[which.min(cars$MPG),] will return the value which the minimum MPG in the cars vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

colClasses

A

an argument in the read.delim & read.table functions. Use this argument to specify the data class of the variables you are importing

ex: read.delim(x, sep = “”, colClasses = c(“character”, “logical”, “numeric”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Hadley’s data import package

A

readr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

read_csv()

A

readr version of read.csv
read_csv(“mydata”)
loads data as a “tibble”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

read.delim for readr

A

read_tsv (tab seperated value)(“potatoes.txt”, col_names = c(“type”, “weight”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

col_types

A

argument to specify the variable classes in readr package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

read_delim

A

the main import function in the readr package. Similiar to read.table
Must specify the file and delim arguments
ex/ read_delim(“cars.txt”, delim = “/t”, col_names = c(“automaker”,”mpg”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

skip

A

skip rows in your import functions.

ex: skip = 5 will skip the first 5 rows and then begin reading in data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

n_max

A

specifies the number of rows you want to read in, often used with skip

ex: read_delim(“cars.txt”, delim = “/”, skip = 2, n_max = 3)
skips the first two rows and reads in rows 3,4, and 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

readxl

A

Haddley’s excel data import package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

function to list different sheets in excel: readxl package

A

excel_sheets()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

read_excel()

A

import excel data into R

17
Q

import data from the second sheet in an excel doc

A

read_excel(“cars.xls”, sheet = 2)

18
Q

pop_list

A

Utilize lapply with readxl functions to read in all sheets in an excel file at once. Must specify the path as a separate argument because the excel_sheets argument only lists the sheets in the file, it does not list the file path

19
Q

col_types

A

argument in read_excel. specify the data type of columns “text”, “numeric”, “date”, “blank”

20
Q

col_types = (“blank”)

A

read_excel will skip the import of a column with “blank” as col_type.
ex: read_excel(“my.data.xlsx”, col_types = c(“numeric”, “blank”)) will only import column 1 as a numeric column from the excel document

21
Q

read_excel(“data.xlsxl”, skip = 2)

A

skip the first two rows of an excel document and then begin importing data

22
Q

XLConnect

A

a package that creates a bridge between r session and excel

23
Q

XLConnct function that builds a bridge betwen R and excel

A

loadWorkbook()

24
Q

XLConnect function lists the available sheets in an excel workbook. Requires an XLConnect workbook object as first argument (created through loadWorkbook() function)

A

getSheets()

25
Q

XLConnect function loads worksheets in as data. Requires an XLConnect workbook object as first argument

A

readWorksheet()

26
Q

arguments in readWorksheet( my_book, startCol = 1, endCol = 3, starRow = 1, endRow = 3)

A

import in data from the “my_book” workbook object starting in column 1 and ending in column 3. ie only import data from column 1:3 and row 1:3

27
Q

XLConnect function to add new sheets to an excel workbook object (the bridget created through loadWorkbook())

A

createSheet( workbook object, “new_SheetName”)

28
Q

add data to an XLConnect workbook

A

writeWorksheet(workbook object, new data, “sheet_to_write_to)

29
Q

save an XLConnect workbook to a new file

A

saveWorkbook(my_workbook, “filename.xlsx”)

30
Q

rename a sheet using XLConnect

A

renameSheet(my_workbook, sheet = 1, newName = “cars”)