Your SlideShare is downloading. ×
20130222 Data structures and manipulation in R
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

20130222 Data structures and manipulation in R


Published on

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Manipulationg data in 2013-02-22 @HSPH Kazuki Yoshida, M.D. MPH-CLE student FREEDOM TO  KNOW
  • 2. Manipulating data in Rn What are Objects?n What is Class attribute?n Various data objects you will see in R.
  • 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. 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. Classes of data values inside data objectsn Numeric: Continuous variablesn Factor: Categorical variablesn Logical: TRUE/FALSE binary variablesn etc...
  • 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. Object iables var ical ! gor ide C ate ins Class attribute
  • 8. Data objectsn Vector (contains single class of data values)n List (contains multiple classes of data values)
  • 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. 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. Vector1-dimensional
  • 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. MatrixFolded vector with dimension
  • 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. List Multi-part object Can contain vectors, arrays, or lists!
  • 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. Data FrameMultiple vectors of same length tied together!
  • 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. Access named elementsn list3n list3$smalln list3[["small"]]n df1$largen df1[, "large"]