Manipulationg data in 2013-02-22 @HSPH Kazuki Yoshida, M.D. MPH-CLE student FREEDOM TO KNOW
Manipulating data in Rn What are Objects?n What is Class attribute?n Various data objects you will see in R.
Objectsn Just about everything named in R is an objectn An object is a container that n knows its class (label for what’s inside). n has contents (eg, Actual numbers).
Examples of objectsn dataset, which you use for analysis (various classes)n functions, which perform analysis (function class)n results, which come out of analysis (various classes) n In effect, you always get a new dataset ﬁlled with results when you analyze data.
Classes of data values inside data objectsn Numeric: Continuous variablesn Factor: Categorical variablesn Logical: TRUE/FALSE binary variablesn etc...
Class?n An object’s class tells R how the object should be handled.n For example, summarizing data should work differently for numbers and categories!
Object iables var ical ! gor ide C ate ins Class attribute http://en.wikipedia.org/wiki/File:3_D-Box.jpg
Data objectsn Vector (contains single class of data values)n List (contains multiple classes of data values)
Data objectsn Vector (contains single class of data values) n Array including Matrixn List (contains multiple classes of data values) n Data frame
Vectorn Smallest building block of data objectsn Single dimensionn Combination of values of same classn vec1 <- c(2013, 2, 15, -10) # combinen vec2 <- 1:16 # integers 1 to 16
Listn Combination of any values or objectsn Can contain objects of multiple classesn eg, a list of two vectors, a matrix, three arraysn List_name$Variable_name operation with $ operatorn list1 <- list(ﬁrst = 1:17, second = matrix(letters, 13,2))n list2 <- list(alpha = c(1,4,5,7), beta = c("h","s","p","h"))
List Multi-part object Can contain vectors, arrays, or lists!
Data framen Special case of a listn List of same-length vectors vertically alignedn df1 <- data.frame(list2)n list3 <- list(small = letters, large = LETTERS, number = 1:26)n df2 <- data.frame(list3)
Data FrameMultiple vectors of same length tied together!