20130222 Data structures and manipulation in R

1,028 views
898 views

Published on

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,028
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
24
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

20130222 Data structures and manipulation in R

  1. 1. Manipulationg data in 2013-02-22 @HSPH Kazuki Yoshida, M.D. MPH-CLE student FREEDOM TO  KNOW
  2. 2. Manipulating data in Rn What are Objects?n What is Class attribute?n Various data objects you will see in R.
  3. 3. 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).
  4. 4. 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 filled with results when you analyze data.
  5. 5. Classes of data values inside data objectsn Numeric: Continuous variablesn Factor: Categorical variablesn Logical: TRUE/FALSE binary variablesn etc...
  6. 6. 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!
  7. 7. Object iables var ical ! gor ide C ate ins Class attribute http://en.wikipedia.org/wiki/File:3_D-Box.jpg
  8. 8. Data objectsn Vector (contains single class of data values)n List (contains multiple classes of data values)
  9. 9. Data objectsn Vector (contains single class of data values) n Array including Matrixn List (contains multiple classes of data values) n Data frame
  10. 10. 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
  11. 11. Vector1-dimensional
  12. 12. Array/Matrixn Vector folded into a multidimensional structuren 2-dimensional array is a matrixn vec3 <- 1:16n dim(vec3) <- c(4, 4) # 4 x 4 structuren dim(vec3) <- c(2, 2, 4) # 2 x 2 x 4 structuren arr1 <- array(1:60, dim = c(3,4,5))
  13. 13. MatrixFolded vector with dimension
  14. 14. 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(first = 1:17, second = matrix(letters, 13,2))n list2 <- list(alpha = c(1,4,5,7), beta = c("h","s","p","h"))
  15. 15. List Multi-part object Can contain vectors, arrays, or lists!
  16. 16. 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)
  17. 17. Data FrameMultiple vectors of same length tied together!
  18. 18. Access by indexesn letters[3] # 1-dimensional objectn arr1[1,2,3] # 3-dimensional objectn arr1[1, ,3] # implies 1,(all),3n df1[ ,3] # implies (all),3n list1[[1]] # list needs [[ ]]
  19. 19. Access named elementsn list3n list3$smalln list3[["small"]]n df1$largen df1[, "large"]

×