2 L02: Basics I
2.1 Basics of R: Data Structures and Subsetting
2.2 Goals
- Getting to know main data structures in R
2.3 Software{#02}
2.4 Class
- Practice worksheet:
02_worksheets_data-structures.Rmd.zip
NB: Original worksheets prepared by Lincoln Mullen, GMU (https://dh-r.lincolnmullen.com/worksheets.html)
2.5 Topics: Data Structures & Types
- Data structures
- Vectors
- Matrices
- Data frames
- Lists
- Subsetting operations
2.5.1 Additional notes
str()
compactly dysplays information about an R objecttypeof()
determines the internal type or storage mode of an R objectclass()
tells you the data structure of an R object:list
data.frame
matrix
- vector
numeric
typeof()
>double
— a double-precision vector (floats; default numberic vector type)typeof()
>integer
— a vector of integers
character
typeof()
>character
— a vector of strings/characters
- Checking class/type & Conversion
- create
test <- c(1,2,3,4,5)
is.___
tests whether a variable of a___
class.- or type: check the
typeof()
ourtest
vector; then trytest.ch <- as.character(test)
and check the type again; can you convert it back into its initial type?).
- or type: check the
is.___
converts into a___
class.- (
___
is either:vector
,matrix
,data.frame
,list
)
- Now try the following commands and check the type and class of the new objects:
test.matrix <- as.matrix(test)
test.df <- as.data.frame(test)
test.list <- as.list(test)
test.vector <- as.vector(test.df$test)
- create
2.6 Reference materials
- Read two chapters from: Wickham, Hadley. Advanced R, Second Edition. 2 edition. Boca Raton: Chapman and Hall/CRC, 2019. http://adv-r.had.co.nz/
- For the next class, read the following article (in open access, simply follow the link):
- Broman, Karl W., and Kara H. Woo. 2018. “Data Organization in Spreadsheets.” The American Statistician 72 (1): 2–10. https://doi.org/10.1080/00031305.2017.1375989.
- Additional readings:
- Read Introduction to Lincoln A. Mullen, Computational Historical Thinking: With Applications in R (2018–): (http://dh-r.lincolnmullen.com/introduction.html)
2.7 Homework
- Finish your worksheet and submit your HW as described below.
2.8 Submitting homework
- Homework assignment must be submitted by the beginning of the next class;
- Email your homework to the instructor as attachments.
- In the subject of your email, please, add the following:
070184-LXX-HW-YourLastName-YourMatriculationNumber
, whereLXX
is the numnber of the lesson for which you submit homework;YourLastName
is your last name; andYourMatriculationNumber
is your matriculation number.
- In the subject of your email, please, add the following: