{"id":102,"date":"2014-02-15T20:38:33","date_gmt":"2014-02-15T20:38:33","guid":{"rendered":"http:\/\/kktg.net\/sgr\/?page_id=102"},"modified":"2014-02-27T18:20:44","modified_gmt":"2014-02-27T18:20:44","slug":"sgr-code-1","status":"publish","type":"page","link":"http:\/\/kktg.net\/sgr\/sgr-code-2\/sgr-code-1\/","title":{"rendered":"SGR Code from Chapters 1-6"},"content":{"rendered":"<h3>R Code for Chapter 1: Introduction<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n#===============================================================================\r\n# Chapter 1 -- Introduction\r\n#===============================================================================\r\n\r\n# 1.1a - R commands ======================================================= 1.1a\r\n\r\n# This comment identifies the purpose of this R-script\r\n# It is really just to demonstrate what R commands look like.\r\n\r\n# R Doesn't care about blank lines. My first command will create an\r\n# x variable and give it a value. Although it is reasonabley obvious,\r\n# I'll also include a comment on the right explaining what I am doing.\r\n\r\nmyX1 = 7                               # This assigns the value 7 to myX\r\n\r\nmyX2 = c(1, 5, 3, 8, 2, 4)             # Create myX2 with a set of values \r\n\r\nmean(myX2)                             # Find the mean for myX2\r\n\r\n# Voila! Our first R program. Now we can highlight those commands and \r\n# use the \"Run\" button to send them to the R console for execution.\r\n\r\n\r\n# 1.3b Command conventions ================================================ 1.3b\r\nmyVar = c(1, 2, 4, NA, 7)              # Create a variable w\/some data\r\nmean(myVar, na.rm = T)                 # Mean of myVar w\/o missing values\r\nlength(myVar)                          # Num of observations in myVar\r\nls()                                   # List all objects\r\n\r\n\r\n# Command short and alternative forms\r\nmyRandNum = rnorm(1)                   # A single random number from a normal\r\n                                       #   distribution assuming defaults\r\nmyRandNum = rnorm(1, 0, 1)             # The same thing showing defaults\r\n\r\nmyRandNum = rnorm(1,                   # A random number from normal dist\r\n  mean = 0,                            #   with mean = 0    \r\n  sd = 1)                              #   and std dev = 1\r\n\r\nmyRandNum = rnorm(n = 1,               # A random number from normal dist \r\n  sd = 1,                              #   with std dev = 1\r\n  mean = 0)                            #   and mean = 0\r\n\r\n# Using help examples ----------------------------------------------------------\r\n\r\nexample(mean)                          # Run example from mean() help page\r\n\r\n\r\n# Note:  The other examples from Chapter 1 are not included here, as they aren't\r\n# really meant to be executed.\r\n\r\n<\/pre>\n<h3>R Code for Chapter 2: A Sample Session<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n#===============================================================================\r\n# Chapter 2 -- A sample session\r\n#===============================================================================\r\n\r\noptions(width = 75)                    # Limit output width to fit book format  \r\n \r\n# Read data from web  (Note - this will change to a data site on\r\n# www.sagepub.com\/gaubatz\r\n\r\nmyDF = read.delim(\"http:\/\/www.kktg.net\/R\/Chapter2Data.txt\", \r\n  header = TRUE,                       # Data has headers for var names\r\n  colClasses = c(\"character\",          # Set storage modes of variables\r\n    \"numeric\", \"numeric\", \"numeric\",   # This is just a convenience and  \r\n    \"numeric\", \"factor\", \"numeric\"))   #   could be adjusted afterwards\r\n\r\nsummary(myDF)                          # Summarize dataset\r\n\r\n# Standard deviations ----------------------------------------------------------\r\nsd(myDF$GDPc, na.rm = T)               # Get standard deviations\r\nsd(myDF$MilSpend, na.rm = T)           #   removing missing values\r\nsd(myDF$EdSpend, na.rm = T)\r\nsd(myDF$FemLife, na.rm = T)\r\nsd(myDF$PressStat, na.rm = T)\r\nsd(myDF$PressFree, na.rm = T)          # Note R will do sd of a factor\r\n\r\n# A simple plot ----------------------------------------------------------------\r\npng(filename = \"illustrations\/fig-2-1-femlife plot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))\r\n\r\nplot(myDF$PressStat, myDF$FemLife)     # Plot: femlife by free press\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Hypothesis testing -----------------------------------------------------------\r\n# Check equality of variances\r\n\r\nvar.test(                              # Run a test comparing variances\r\n  myDF$FemLife[myDF$PressFree == \"F\"], # FemLife in states w\/ free press\r\n  myDF$FemLife[myDF$PressFree == \"NF\"])#  compared to states w\/o free press\r\n\r\n# t-test -----------------------------------------------------------------------\r\nt.test(                                # A t-test for the hypothesis that\r\n  myDF$FemLife[myDF$PressFree == \"F\"], #  states w\/ press freedom and \r\n  myDF$FemLife[myDF$PressFree == \"NF\"])#  states w\/o press freedom have\r\n                                       #  the same female life expectancy\r\n\r\n# A simple boxplot -------------------------------------------------------------\r\npng(filename = \"illustrations\/fig-2-2-femlife boxplot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(.5, .5, .25, .25))         # Set margins - no Title, no x label\r\n\r\nboxplot(myDF$FemLife ~ myDF$PressFree) # Boxplot: femlife by free press status\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Look at states missing press freedom status ----------------------------------\r\n\r\nmyDF$Country.name[myDF$PressFree == \"\" # List states missing press status\r\n  & !is.na(myDF$FemLife)]              #  but not missing f life exp.\r\n\r\n# Getting the factor levels sorted ---------------------------------------------\r\n# Here we want to have our levels of press freedom displayed in substantive \r\n# rather than alphabetical order---that is Free, Partly Free, Not Free, rather\r\n# than the alphabetical order. We also want missing values (NAs) to be treated\r\n# as missing rather than as another discrete level.\r\n\r\nstr(myDF$PressFree)                    # Show current structure of factor\r\nmyDF$PressFree =                       # Reorder factor levels in\r\n  factor(myDF$PressFree,               #  substantive rather than \r\n  levels = c(\"F\", \"PF\", \"NF\"))         #  alphabetical order\r\nstr(myDF$PressFree)                    # Show current structure of factor\r\n\r\n# A simple multi-variate model -------------------------------------------------\r\n\r\nmodel1 = lm(FemLife ~ GDPc + MilSpend + PressFree, data = myDF)\r\nsummary(model1)\r\n\r\n# Another simple plot ----------------------------------------------------------\r\npng(filename = \"illustrations\/fig-2-3-femlife GDPc plot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\nplot(myDF$GDPc, myDF$FemLife)          # Plot: femlife by GDPc\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Once more, with a log transformation -----------------------------------------\r\npng(filename = \"illustrations\/fig-2-4-femlife log GDPc plot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\nplot(log(myDF$GDPc), myDF$FemLife)     # Plot: femlife by ln of GDPc\r\n\r\ndev.off()                              # Output png file\r\n\r\n# A simple multi-variate model with log transformations ------------------------\r\n\r\nmodel2 = lm(FemLife ~ log(GDPc) + log(MilSpend) + PressFree, \r\n  data = myDF)\r\nsummary(model2)\r\n\r\n<\/pre>\n<h3>R Code for Chapter 3: R Objects<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n#===============================================================================\r\n# Chapter 3 -- R Objects\r\n#===============================================================================\r\n\r\n# 3.1 R Objects and their Names ============================================ 3.1\r\n\r\n# 3.2 How to think about data objects in R ================================= 3.2\r\n\r\n# Figure 3.1: A graphic representation of my R objects approach ----------------\r\n\r\n# This is just an exercise in drawing with R. The code doesn't really serve\r\n# much purpose beyond that.  But, if you are needing to do work with shapes\r\n# following the lead from Chapter 14, you might find some bits of use.\r\n\r\n# This starts with the custom ovals function from chapter 10 and the\r\n# custom arrows function from chapter 14 already loaded.\r\n\r\npng(filename = \"illustrations\/fig-3-1-R objects schema.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(0, 0, 0, 0))               # Set margins - no Title\r\npar(usr = c(0, 1, 0, 1))               # Set coordinates to 0,1 space\r\n                   \r\nplot.new()                             # Start a new plot\r\n\r\noval(xcen = .55, ycen = .5,            # Top oval for obj type container\r\n  xlen = .175, ylen = .025,            # Length of radii\r\n  ewidth = 2,                          # Line width\r\n  ecolor = \"gray\")                     # Line color\r\n\r\nmyArrow(x0 = .26, x1 = .445,           # Arrow from storage mode to object\r\n  y0 = .75, y1 = .75,\r\n  angle = 45,                          # Set arrowhead angle\r\n  ljoin = 1,                           # Set line join type\r\n  lwd = 25,                            # Set line join type\r\n  col = \"light gray\")                  # Set color\r\n\r\npolygon(                               # Arrow body\r\n  x = c(.5, .5, .6, .6),\r\n  y = c(.55, .7, .7, .55),\r\n  col = \"light gray\",\r\n  border = \"light gray\")\r\n  \r\npolygon(                               # Arrow head\r\n  x = c(.55, .475, .625),\r\n  y = c(.49, .55, .55),\r\n  col = \"light gray\",\r\n  border = \"light gray\")\r\n\r\nmyArrow(x0 = .8, x1 = .655,            # Arrow from class to object\r\n  y0 = .85, y1 = .75,\r\n  L = .25,                             # Set length of arrowhead\r\n  angle = 30,                          # Set arrowhead angle\r\n  ljoin = 2,                           # Set line join type\r\n  lwd = 10,                            # Set line join type\r\n  col = \"light gray\")                  # Set color\r\n    \r\npolygon(                               # Rectangle around Storage Mode\r\n  x = c(.001, .001, .249, .249),\r\n  y = c(.45, .99, .99, .45),\r\n  col = \"white\",\r\n  lwd = 2,\r\n  border = \"gray\")\r\n    \r\npolygon(                               # Rectangle around pseudo-Storage Mode\r\n  x = c(.001, .001, .249, .249),\r\n  y = c(.45, .625, .625, .45),\r\n  col = gray(.8),\r\n  lwd = 2,\r\n  border = \"gray\")\r\n  \r\npolygon(                               # Rectangle around Class\r\n  x = c(.8, .8, .95, .95),\r\n  y = c(.8, .9, .9, .8),\r\n  col = \"white\",\r\n  lwd = 2,\r\n  border = \"gray\")\r\n                                       \r\nsymbols(x = .55, y = .75,              # Filled circle behind R Data Object\r\n  circles = .1,                        # Set circle size\r\n  inches = FALSE,                      # Don't constrain to inch size\r\n  add = TRUE,                          # Add to current plot\r\n  bg = \"white\")                        # Set background color\r\n\r\noval(xcen = .55, ycen = .75,           # Circle around R Data Object in center\r\n  xlen = .1, ylen = .1*(3\/2),          # Y radius adjusted for aspect ratio\r\n  ewidth = 6,                          # Set line size\r\n  ecolor = \"gray\")                     # Set color\r\n\r\ntext(x = .55, y = .75,                 # R Data Object Text\r\n  labels = \"R Data\\nObject\",\r\n  cex = 1.5,                           # Set font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")                    # Serif Font\r\n\r\ntext(x = .55, y = .375,                # Data Object Type Text\r\n  labels = \"Data Object Type\",\r\n  pos = 3,                             # Above and centered\r\n  cex = 1.25,                          # Set font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")                    # Serif Font\r\n\r\ntext(x = .55, y = .325,                # List of Data Object Types\r\n  labels = \"Vector\\nMatrix\\nData Frame\\nList\",\r\n  pos = 1,                             # Below and centered\r\n  cex = 1,                             # Set font size\r\n  family = \"sans\")                     # Sans serif font\r\n\r\ntext(x = .125, y = .9,                 # Data Object Storage Mode Text\r\n  labels = \"Storage Mode\",\r\n  pos = 3,                             # Above and centered\r\n  cex = 1.25,                          # Set font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")                    # Serif Font\r\n\r\ntext(x = .125, y = .85,                # Storage Modes List\r\n  labels = \"Logical\\nNumeric\\nCharacter\\n\\n\\nDate\/Time\\nFactor\",\r\n  pos = 1,                             # Below and centered\r\n  cex = 1,                             # Set font size\r\n  family = \"sans\")                     # Sans serif font\r\n  \r\ntext(x = .875, y = .85,                # Data Object Class Text\r\n  labels = \"Class\",\r\n  cex = 1.25,                          # Set font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")                    # Serif Font\r\n\r\n\r\noval(xcen = .55, ycen = .05,           # Bottom oval for obj type container\r\n  xlen = .175, ylen = .025,\r\n  ewidth = 2,\r\n  ecolor = \"gray\")\r\n \r\nsegments(x0 = .375, x1 = .375,         # Left line for object type container\r\n  y0 = .05, y1 = .5,\r\n  lwd = 2,\r\n  col = \"gray\")\r\n  \r\nsegments(x0 = .725, x1 = .725,         # Right line for object type container\r\n  y0 = .05, y1 = .5,\r\n  lwd = 2,\r\n  col = \"gray\")   \r\n\r\ndev.off()                              # Output png file\r\n\r\n# 3.3 R Object storage modes =============================================== 3.3\r\n\r\n# Create some diverse objects\r\n\r\n# Non-Data objects                     # A function to add 10 to any number\r\nmyFunction = function(x){\r\n  return(x + 10)\r\n} \r\nmyModel =                              # Output from a regression model\r\n  lm(c(1:10) ~ c(1, 3, 2, 5, 4:9))   \r\n\r\n# Data objects\r\nmyInteger = as.integer(4)              # An integer (whole number)\r\nmyWholeNumber = 5                      # A whole number stored as double\r\nmyDouble = 3.7                         # A numeric-double number\r\nmyOtherInteger = as.integer(3.7)       # Non-whole num converted to integer\r\nmyLogical1 = TRUE                      # A logical value set to TRUE\r\nmyLogical2 = FALSE                     # A logical value set to FALSE\r\nmyCharacter = \"Hello World!\"           # A character string\r\n\r\n\r\ntypeof(myInteger)                      # Test \"typeof\" for integer\r\nmode(myInteger)                        # Test \"mode\" for integer\r\ntypeof(myWholeNumber)                  # Test \"typeof\" for whole number\r\ntypeof(myDouble)                       # Test \"typeof\" for double\r\nmode(myDouble)                         # Test \"mode\" for double\r\nmyOtherInteger                         # Show double (3.7) converted to integer\r\ntypeof(myOtherInteger)                 # Show \"typeof\" for converted to integer\r\nmode(myOtherInteger)                   # Show \"mode\" for convert to integer\r\ntypeof(myLogical1)                     # Test \"typeof\" for logical\r\ntypeof(myCharacter)                    # Test \"typeof\" for character\r\n\r\n# 3.4 R data object Types ================================================== 3.4\r\n\r\nmyVector = c(1:5)\r\nmyDF = data.frame(c(1:5), letters[1:5], LETTERS[1:5])\r\nmyList = list(c(1, 2, 3), \"This is a list\", TRUE)\r\nmyList = list(\"This is a list with my Dataframe\", myDF)\r\n\r\nDOType = function(x){                  # DOType function ----------------------+\r\n# This is a function to identify data object types. I think                    |\r\n# of object type as a characterization of objects that hold collections        |\r\n# of things.  These object types are vectors, matrices, data frames,           |\r\n# lists, and factors.                                                          |\r\n# If none of those types fit, then the function returns a statement            |\r\n# that it is not a recognized data type.                                       |\r\n#                                                                              |\r\n  DOT = \"\"                             # Set default value for DOT             |\r\n  if(is.vector(x)){DOT = \"vector\"}     # Check if is a vector                  |\r\n  if(is.matrix(x)){DOT = \"matrix\"}     # Check if is a matrix                  |\r\n  if(is.data.frame(x)){                # Check if is a data frame              |\r\n    DOT=\"data frame\"}                  #                                       |\r\n  if(is.list(x) & !is.data.frame(x)){  # Check if is a list (and not a         |\r\n    DOT=\"list\"}                        #   dataframe)                          |\r\n                                       #                                       |\r\n  if(DOT == \"\"){DOT = paste(\"Not a\",   # Print a message if it is none         |\r\n    \"recognized data object\")}         #   of the above                        |\r\n  return(DOT)                          # Return the appropriate value          |\r\n}                                      # End of function ----------------------+\r\n\r\n# 3.5 The basic objects: Vectors =========================================== 3.5\r\n\r\nmyLogicalVector =  c(T, F, T, T)       # Set up a logical vector\r\nmyNumericVector = c(1, 2, 4, 7)        # Set up an integer vector\r\nmyTextVector = c(\"a\", \"b\", \"7\", \"x\")   # Set up a text vector\r\ntypeof(myLogicalVector)                # Show type for logical vector\r\ntypeof(myNumericVector)                # Show type for numeric vector\r\nmode(myTextVector)                     # Show type for text vector\r\n\r\n# ------------------------------------------------------------------------------\r\nmyScalar = 1                           # Create a scalar with value 1\r\nyourScalar = 2                         # Create a scalar with value 2\r\nmyScalar + yourScalar                  # Add the two scalars\r\n\r\n# ------------------------------------------------------------------------------\r\n\r\nmyVector1 = c(0, 5, 18)                # Set up a numeric vector\r\ntypeof(myVector1)                      # Check vector type\r\nmyVector2 = c(TRUE, TRUE, FALSE)       # Set up a logical vector\r\ntypeof(myVector2)                      # Check vector type\r\nmyVector3 = c(\"Fred\", \"Joe\", \"Simon\")  # Set up a character vector\r\ntypeof(myVector3)                      # Check vector type\r\nmyVector1 + myVector3                  # Add num & char vectors (error)\r\nmyVector1 + myVector2                  # Add numeric and logical vectors\r\nas.logical(myVector1)                  # Treat numeric vector as logical\r\n\r\n\r\n# 3.5a Vector indices ===================================================== 3.5a\r\n\r\nmyVector = c(3, 9, 5)                  # Set up vector of numeric values\r\nmyVector[2]                            # Get the vector's 2nd element\r\nmyVector[3]                            # Get the vector's 3rd element\r\n\r\n# ------------------------------------------------------------------------------\r\n\r\nmyVector = c(3, 9, 5)                  # Set up vector of numeric values\r\nmyIndex = 1                            # Set up a selection index\r\nmyVector[myIndex]                      # Use index for selection\r\n\r\n# ------------------------------------------------------------------------------\r\n\r\nmyVector = c(\"Bob\", \"Mary\", \"Fred\")    # Set up vector of character values\r\nmyVector[2]                            # Show second element\r\nmyIndex = c(1, 3)                      # Set up an index variable\r\nmyVector[myIndex]                      # Select vector elements w\/index\r\n\r\n# 3.5b Vector operations ================================================== 3.5b\r\n\r\n# Vectorized operation\r\nmyVector1 = c(1, 2, 3)                 # Create vector with 3 elements\r\nmyVector2 = log(myVector1)             # Create new vector with log \r\nmyVector2                              # Print new vector\r\n\r\n# ------------------------------------------------------------------------------\r\n\r\n# non-vectorized operation\r\nmyVector = c(1, 2, 3)                  # Numeric vector with 3 elements\r\nif(myVector == 1) print(\"Answer is 1\") # This produces a likely error\r\nif(myVector[1] == 1)                   # This time we specify the first element\r\n  print(\"Answer is 1\")\r\nmyAnswer = ifelse(myVector == 1,       # Here is the vectorized ifelse()\r\n  \"Answer is 1\",                       #   it operates on each element in\r\n  \"Answer is not 1\")                   #   the vector individually\r\nmyAnswer                               # Show results\r\n\r\n# ------------------------------------------------------------------------------\r\n\r\n# Whole vector operations\r\nmyVector = c(1, 2, 3)                  # Create a vector\r\nmin(myVector)                          # Get the vector minimum\r\nmax(myVector)                          # Get the vector maximum\r\nsum(myVector)                          # Sum the vector elements\r\n\r\n# ------------------------------------------------------------------------------\r\n\r\n# Vector math\r\nmyVector1 = c(1, 2, 3)                 # A vector with 3 elements\r\nmyVector2 = c(10, 20, 30)              # Another 3 element vector\r\nmyVector1 + myVector2                  # Vector addition\r\nmyVector1 * myVector2                  # Vector multiplication\r\nmyVector2\/myVector1                    # Vector division\r\nmyVector5 = c(10, 20, 30, 40, 50)      # A vector with 5 elements\r\nmyVector1 + myVector3                  # Add different length vectors (error)\r\nmyVector6 = c(10, 20, 30, 40, 50, 60)  # A vector with 6 elements\r\nmyVector1 + myVector6                  # Add different length vectors when one\r\n                                       # length is a multiple of the other\r\n\r\n# 3.6 The basic objects: Matrices and their indices ======================== 3.6\r\n\r\n# Matrix math is not the same as matrix algebra!\r\nmyMatrix = rbind(c(3, 8), c(23, 33))   # Create matrix by binding two rows\r\nmyMatrix                               # Display myMatrix\r\nmyMatrix1 = myMatrix + 7               # Add 7 to each element of myMatrix\r\nmyMatrix1                              # Display myMatrix1\r\n\r\nmyMatrix2 = rbind(c(2, 5), c(3, 2))    # Create another matrix by binding rows\r\nmyMatrix2                              # Display myMatrix2\r\nmyMatrix3 = myMatrix1 \/ myMatrix2      # Divide myMatrix1 by myMatrix2\r\nmyMatrix3                              # Display myMatrix3\r\nmyMatrix4 = myMatrix2 %*% myMatrix3    # Use matrix algebra multiplication\r\nmyMatrix2                              # Display myMatrix2\r\nmyMatrix3                              # Display myMatrix3\r\nmyMatrix4                              # Display myMatrix4\r\n\r\n# 3.7 The basic objects: Data frames ======================================= 3.7\r\n\r\n# Row and column names\r\n\r\nrownames(myData) =                     # Add row names \r\n  c(\"Mary\", \"Mike\", \"Mia\", \"Mish\", \"Mark\")\r\ncolnames(myData) = c(\"sex\", \"age\")     # Add column names \r\ncolnames(mydata) = mydata[1,]          # Set col names to values in 1st row\r\n                                       # Note that this keeps first row as \r\n                                       #   first obs in the data frame \r\n\r\n\r\n# 3.7a Referencing data frame elements ==================================== 3.7a\r\n\r\n# Attach and other methods =====================================================\r\n# Set up a data frame\r\nmyDF = data.frame(                     # Create a data frame\r\n  myVar1 = c(seq(0, 100, by = 5)),     # Set up variable 1\r\n  myVar2 = c(0:20))                    # Set up variable 2\r\n\r\n# 1. the $ construction --------------------------------------------------------\r\n\r\nmean(myDF$myVar1)                      # This always works & minimizes errors\r\n\r\n# 2. The attach() method -------------------------------------------------------\r\n\r\nattach(myDF)                           # Attach the data frame\r\n  mean(myVar2)                         # Do something with the data frame\r\n  myVar2 = myVar2 * 2                  # Here is a transformation\r\n  mean(myVar2)                         # Mean of transformed variable\r\ndetach(myDF)                           # Detach the data frame\r\nmean(myDF$myVar2)                      # Transform lost outside of attach\r\n\r\n# 3. The data= method ----------------------------------------------------------\r\n\r\nlm(myVar1 ~ myVar2, data = myDF)       # Do something with the data frame again\r\n\r\n# 4. The with() method ---------------------------------------------------------                  \r\n\r\nwith(myDF, {                           # Use with() to indicate data frame\r\n  myVar1 = myVar1\/2                    # Transform myVar1\r\n  sd(myVar1)                           # Std dev. of transformed myVar1\r\n})                                     # Note close brace & close paren finish\r\nsd(myDF$myVar1)                        # Transform lost outside with()\r\n\r\n# 3.7b Displaying the contents of a data frame ============================ 3.7b\r\n\r\nmyVar1 = c(\"a\", \"b\", \"c\")              # Create char variable\r\nmyVar2 = c(10, 11, 12)                 # Create numeric variable\r\nmyDF = data.frame(myVar1, myVar2)      # Combine into data frame\r\nmyDF                                   # Print data frame\r\nnames(myDF)                            # Show names of variables in myDF\r\nsummary(myDF)                          # Summarize variables in data frame\r\n\r\n# 3.8 The basic objects: Lists ============================================= 3.8\r\n\r\n# A list generated by the lm model ---------------------------------------------\r\n\r\nmyVar1 = c(1:8)                        # Set up a y variable\r\nmyVar2 = c(3, 5, 4, 6, 7, 9, 2, 9)     # Set up an x variable\r\nmyModel = lm(myVar2 ~ myVar1)          # Create a linear model\r\nmyModel                                # Print the model output\r\nattributes(myModel)                    # Show the elements in the model output\r\nmyModel$residuals                      # Show the residuals from the model\r\n\r\n# Double brackets v. Single bracket list indexing ------------------------------\r\n\r\nmyModel[2]                             # Single bracket index result\r\nmyModel[[2]]                           # Double bracket index result\r\n\r\nmyModel[2][1]                          # Single [] won't open list item\r\nmyModel[[2]][1]                        # Double [[]] allows list item access\r\nmyModel$residuals[1]                   # $ referencing works the same way\r\n\r\n# Create a list ----------------------------------------------------------------\r\n\r\nmyList = list(1,                       # List starting with model num (1)\r\n  myModel,                             # Then the myModel list \r\n  \"This is a discussion of myModel\")   # Then some discussion of myModel\r\n\r\nnames(myList) = c(\"ModelNumber\",       # Create the list names\r\n  \"ModelOutput\",\"ModelDiscussion\")\r\nmyList                                 # Show myList\r\nmyList$ModelOutput$residuals           # Residuals from myModel in myList\r\n\r\n# unlist() ---------------------------------------------------------------------\r\n\r\nmyList = list(c(1,2,3),c(\"a\",\"b\",\"c\"),\"It's numbers and letters!\")\r\ntypeof(myList)                         # Show object type for myList\r\nmyList                                 # Print myList\r\nmyNotList = unlist(myList)             # New object = unlisted myList\r\ntypeof(myNotList)                      # Show type of new unlisted object\r\nmyNotList                              # Show my new object\r\n\r\n# 3.9 A few things about working with objects ============================== 3.9\r\n\r\n# Listing active objects -------------------------------------------------------\r\nobjects()                              # Show all active objects\r\n\r\n# Removing objects -------------------------------------------------------------\r\nmyVector = 1:10                        # Create some objects\r\nmyNewVector = myVector+3\r\nmyAnimal = \"aardvark\"\r\nobjects()                              # List the objects\r\n\r\nrm(myVector)                           # Remove an object\r\nobjects()                              # List the objects\r\nrm(list=ls())                          # Remove ALL objects\r\nobjects()                              # List the objects\r\n \r\n# Object overwriting -----------------------------------------------------------\r\n\r\nmyNumber = 5                           # Assign value 5 to myNumber\r\nmyNumber = 7                           # Assign value 7 to myNumber\r\nmyNumber                               # Show that 7 replaced 5\r\n\r\nmyNumber = 5                           # Assign value 5 to myNumber\r\nmyNumber = myNumber + 4                # Add 4 to myNumber\r\nmyNumber                               # Show new value for myNumber\r\n\r\n# 3.10 Object Attributes ================================================== 3.10\r\n\r\n# Set attributes for row and column names in a data frame \r\nmyData = data.frame(cbind(             # Create data frame w\/2 vectors\r\n  c(1, 0, 1, 1, 0),                    #   Vector 1\r\n  c(24, 38, 22, 51, 17)))              #   Vector 2\r\n\r\n# The attr approach\r\nattr(myData, \"names\") =                # Set col names to identify vars\r\n  c(\"sex\", \"age\") \r\nattr(myData, \"row.names\") =            # Set row names to identify obs\r\n  c(\"Mary\", \"Mike\", \"Mia\", \"Mish\", \"Mark\")\r\nmyData                                 # Display the data frame\r\n\r\nattr(myData, \"names\")=NULL             # Erase attributes\r\n\r\n# The rownames\/colnames approach\r\nrownames(myData) =                     # Add row names \r\n  c(\"Mary\", \"Mike\", \"Mia\", \"Mish\", \"Mark\")\r\ncolnames(myData) = c(\"sex\", \"age\")     # Add column names \r\n\r\n# Using str(0 to show the structure of a data frame ----------------------------\r\nstr(myData)                            # Show structure of myData data frame\r\n\r\n# 3.11 Objects and environments =========================================== 3.11\r\n\r\n# 3.12 Object classes ===================================================== 3.12\r\n\r\nx = 5                                  # Create an object\r\nclass(x)                               # Show object class\r\nclass(mean)                            # Show class of existing R command\r\nclass(x) = \"my made up class\"          # Set new custom class for x\r\nattributes(x)                          # Show attributes of x\r\nstr(x)                                 # Show structure of x\r\n\r\n# 3.13 The pseudo storage modes =========================================== 3.13\r\n\r\n# 3.14 Date and time as storage modes ===================================== 3.14\r\n\r\n# 3.15 Factors ============================================================ 3.15\r\n\r\n# Figure 3.2 - A graphic showing factor vectors --------------------------------\r\n\r\n# This is another exercise in drawing with R. \r\n# This starts with the custom ovals function from chapter 10 and the\r\n# custom arrows function from chapter 14 already loaded.\r\n\r\npng(filename = \"illustrations\/fig-3-2-R factors.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(0, 0, 0, 0))               # Set margins - no Title\r\npar(usr = c(0, 1, 0, 1))               # Set coordinates to 0,1 space\r\n                   \r\nplot.new()                             # Start a new plot\r\n\r\ntext(x = .15, y = .97,                 # The vector you see text\r\n  labels = \"The vector\\nyou see:\",\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1.5,                           # Set font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")\r\n\r\ntext(x = .15, y = .65,                 # What you see vector\r\n    labels='\r\n\"Small\"\\n\"Small\"\\n\"Large\"\\n\"Large\"\\n\"Large\"\\n\"Small\"\\n\"Small\"',\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1,                             # Font size\r\n  family = \"sans\")\r\n\r\nlines(\r\n  x = c(.09, .075, .075, .09),         # Add brackets for what you see vector\r\n  y = c(.65, .65, .2, .2),\r\n  col = \"black\",\r\n  lwd = 2)\r\n\r\nlines(\r\n  x = c(.21, .225, .225, .21),\r\n  y = c(.65, .65, .2, .2),\r\n  col = \"black\",\r\n  lwd = 2)\r\n\r\nsegments(x0 = .3, x1 = .3,             # Dividing Line\r\n  y0 = .05, y1 = .95,\r\n  lwd = 4,\r\n  col = \"gray\")\r\n\r\ntext(x = .6, y = .97,                  # Vectors R Sees text\r\n  labels = \"The vectors R sees:\",\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1.5,                           # Font size\r\n  font = 2,                               \r\n  family = \"serif\")\r\n\r\ntext(x = .45, y = .89,                 # Levels Vector text\r\n  labels = \"The\\nLevels\\nVector\",\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1,                             # Font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")\r\n\r\ntext(x = .45, y = .625,                # Levels Vector\r\n  labels = 'L1: \"L\"\\nL2: \"S\"',\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1,                             # Font size\r\n  family = \"sans\")\r\n\r\nlines(                                 # Add brackets for levels vector\r\n  x = c(.39, .375, .375, .39),\r\n  y = c(.65, .65, .475, .475),\r\n  col = \"black\",\r\n  lwd = 2)\r\n\r\nlines(\r\n  x = c(.51, .525, .525, .51),\r\n  y = c(.65, .65, .475, .475),\r\n  col = \"black\",\r\n  lwd = 2)\r\n                                       # Pointers vector text\r\ntext(x = .6, y = .89,\r\n  labels = \"The\\nPointers\\nVector\",\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1,                             # Font size\r\n  font = 2,                            # Bold\r\n  family = \"serif\")   \r\n\r\ntext(x = .6, y = .62,                  # Pointers vector\r\n  labels = \"2\\n2\\n1\\n1\\n1\\n2\\n2\",\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1.05,                          # Font size\r\n  family = \"sans\")\r\n\r\nlines(                                 # Add brackets for pointers vector\r\n  x = c(.565, .55, .55, .565),\r\n  y = c(.65, .65, .2, .2),\r\n  col = \"black\",\r\n  lwd = 2)\r\n\r\nlines(\r\n  x = c(.635, .65, .65, .635),\r\n  y = c(.65, .65, .2, .2),\r\n  col = \"black\",\r\n  lwd = 2)\r\n  \r\ntext(x = .75, y = .89,                 # Labels vector text\r\n  labels = \"The\\nLabels\\nVector\\n(optional)\",\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1,                             # Font size\r\n  font=2,                              # Bold\r\n  family = \"serif\")\r\n\r\ntext(x = .75, y = .625,                # Labels vector\r\n  labels = '\"Large\"\\n\"Small\"',\r\n  pos = 1,                             # Place text below and centered\r\n  cex = 1,                             # Font size\r\n  family = \"sans\")\r\n\r\nlines(                                 # Add brackets for labels vector\r\n  x = c(.69, .675, .675, .69),\r\n  y = c(.65, .65, .475, .475),\r\n  col = \"black\",\r\n  lwd = 2)\r\n\r\nlines(\r\n  x = c(.81, .825, .825, .81),\r\n  y = c(.65, .65, .475, .475),\r\n  col = \"black\",\r\n  lwd = 2)\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Factor levels and labels -----------------------------------------------------\r\n\r\nc2ltr = c(\"FR\", \"UK\", \"SW\",            # Create a variable with 2 letter\r\n  \"NK\", \"SO\")                          #   country abbreviations\r\ncname = c(\"France\", \"U.K.\", \"Sweden\",  # Create a variable with\r\n  \"North Korea\", \"Somalia\")            #   country names.\r\n\r\ncountry = c(\"UK\", \"SW\", \"FR\",          # Create dataset w\/country codes\r\n  \"SO\", \"NK\")\r\nregime = c(rep(\"dem\", 3),              #   and democratic status\r\n  rep(\"nondem\", 2))\r\nnations.df =                           # Join into a data frame\r\n  data.frame(regime, country)\r\nnations.df                             # Print the new data frame\r\n\r\n#now add labels                        \r\nnations.df$country =                   # Create a country factor\r\n  factor(nations.df$country,\r\n    levels = c2ltr,                    # Connecting 2 letter codes\r\n    labels = cname)                    #   with country names\r\n\r\nnations.df                             # Print data frame\r\n\r\n# Controlling factors with the factor() command --------------------------------\r\n# Set up the factor from figure 3.2\r\nmyFactor = factor(c(\"S\", \"S\", \"L\", \"L\", \"L\", \"S\", \"S\"))\r\nstr(myFactor)                          # Show factor structure\r\n\r\n# Change the ordering of the levels\r\nmyFactor = factor(myFactor,            # Use values from current factor\r\n  levels=c(\"S\", \"L\"))                  # Specify the levels\r\nstr(myFactor)                          # Show factor structure\r\n\r\n# Add factor labels\r\nmyFactor = factor(myFactor,            # Use values from current factor\r\n  labels=c(\"Small\", \"Large\"))          # Specify labels \r\nstr(myFactor)                          # Show factor structure\r\n\r\n# Add additional level ---------------------------------------------------------\r\nmyFactor = factor(myFactor,            # Use values from current factor\r\n  levels=c(\"Small\", \"Medium\",          # Specify the levels w\/addition\r\n    \"Large\"))  \r\nstr(myFactor)                          # Show factor structure\r\n\r\nlevels(myFactor) =                     # Use levels() to add new level\r\n  c(levels(myFactor), \"X-Large\")       # Combine old levels with new\r\nsummary(myFactor)                      # Summarize myFactor w\/new levels\r\n\r\n# Add new observations\r\nmyFactor = factor(c(myFactor, \"Medium\"),  # Use values from current factor\r\n  levels = c(\"Small\", \"Medium\", \"Large\")) # Specify the levels w\/addition\r\nstr(myFactor)                          # Show factor structure\r\n\r\n# Adding observations ----------------------------------------------------------\r\nmyFactor2 = c(myFactor, \"Medium\")      # Can't concatenate w\/new values\r\nmyFactor2                              # Show result\r\n\r\nmyFactor2 = c(myFactor, 2)             # Can add to pointer vector\r\nmyFactor2                              # But, dumps us out of factor mode\r\nis.factor(myFactor2)\r\n\r\n# The data frame approach to adding observations -------------------------------\r\nmyDF = data.frame(myFactor)            # Put the factor into a data frame\r\nmyDF = rbind(myDF, \"Medium\", \"Small\")  # Add 2 new observations\r\nmyDF                                   # Show result\r\nmyFactor2 = myDF$myFactor              # Return to vector status\r\nmyFactor2                              # Confirm result\r\n\r\n# Combining levels -------------------------------------------------------------\r\n\r\nmyFactor2                              # Display the factor\r\nlevels(myFactor2)                      # Show the current levels\r\nlevels(myFactor2) =                    # Modify the levels to combine\r\n  c(\"Small\", \"Large\", \"Large\")         #   Medium with Large.\r\nmyFactor2                              # Show the new version\r\n\r\n# Ordered and unordered factors ------------------------------------------------\r\n\r\nmySize = c(\"small\", \"medium\",          # Create variable of all sizes\r\n  \"large\", \"X-large\")\r\nsort(mySize)                           # Sort (alphabetical default)\r\nmySize = factor(mySize,                # Set as factor\r\n  levels = mySize,                     # Set factor levels from variable\r\n  ordered = T)                         # Make it an ordered factor\r\n\r\nsort(mySize)                           # Sort (now ordered)\r\n\r\nmyData = c(\"small\", \"large\",           # Here is some data w\/sizes\r\n  \"small\", \"X-large\", \"medium\")\r\nsort(myData)                           # Sort (alphabetical default)\r\nmyData = factor(myData,                # Make it a factor\r\n  levels = mySize)                     # Use levels from mySize\r\nsort(myData)                           # Sort-now based on factor levels\r\n\r\n# 3.16 Coercing storage modes ============================================= 3.16\r\n\r\nmyVector = c(1, 15, 7, \"Smith\")        # Set up a vector\r\ntypeof(myVector)                       # Show type for vector\r\nmyVector[2] + 1                        # Try math w\/2nd element in vector\r\ntypeof(myVector[2])                    # Type for 2nd element in vector\r\nmyVector = as.numeric(myVector)        # Vector forced to numeric\r\nmyVector                               # Print vector\r\ntypeof(myVector)                       # Type for forced numeric vector\r\n\r\n# 3.17 The curse of number-character-factor confusion ===================== 3.17\r\n\r\nmyData = c(7, 8, \"missing\", 8)         # Here we simulate the csv input\r\nsum(myData)                            # If we sum myData we get an error\r\nmyData[1]                              # We see that the value is a character\r\nmyData[1] + 2                          # We get errors with numeric operations\r\nas.numeric(myData[1])                  # Transforming to numeric fixes this\r\nas.numeric(myData[1]) + 2              # Now we can do a numeric operation\r\n\r\n# Character variable factors to numbers ----------------------------------------\r\n                                       \r\nanimal = (c(rep(\"kangas\", 4),          # Create a character variable\r\n  rep(\"koalas\", 5)))\r\nmyData = data.frame(animal)            # Putting the data in a data frame\r\nlevels(myData$animal)                  #  converts character to factor\r\naninum = as.numeric(myData$animal)     # Create a numeric version\r\nmyData = cbind(myData,aninum)          # Add that to the data frame\r\nmyData                                 # Show results\r\n\r\n# The number factor confusion illustrated --------------------------------------\r\n                                       \r\nmyVar = (c(rep(7, 4),                  # Create variable w\/ \"missing\"\r\n  \"missing\", rep(8, 5)))               #  value which forces to character\r\nmyData = data.frame(myVar)             #  which --> factor in data frame\r\nlevels(myData$myVar)                   # Show levels of unwanted factor \r\nmyVar2 = as.numeric(myData$myVar)      # Convert to numeric\r\nmyData = cbind(myData,myVar2)          # Add to data frame\r\nmyData                                 # Show data\r\n\r\n# Factors to characters --------------------------------------------------------\r\nanimal = (c(rep(\"kangas\", 4),          # Create some data\r\n  rep(\"koalas\", 5)))\r\nmyData = data.frame(animal)            # Putting the data in a data frame\r\nmyData$animal[4]                       # Let's take a look at obs 4\r\nmyData$animal[4] = \"koalas\"            # Now we'll change it to \"koalas\"\r\nmyData$animal[4]                       # Another look at num 4 \r\n\r\n# Errors from trying to append new factor levels -------------------------------\r\nmyData$animal[4] = \"hippopotami\"       # When we try to change it to\r\n                                       #   \"hippopotami\" we get a nasty error\r\n\r\n# Solution 1 for the character\/factor conundrum -- using the I() function ------\r\nanimal = (c(rep(\"kangas\", 4),rep(\"koalas\", 5)))\r\nmyData = data.frame(I(animal))         # Putting the data in a data frame\r\n                                       #   but force animal to stay character\r\ntypeof(my.data$animal)                 # Just checking on the datatype\r\nmyData$animal[4]                       # Take a look at obs 4\r\nmyData$animal[4] = \"hippopotami\"       # Now we can make the change\r\nmyData$animal[4]                       # Voila!\r\n\r\n# Solution 2 for the character\/factor conundrum -- adding another factor level -\r\nanimal = (c(rep(\"kangas\", 4),rep(\"koalas\", 5)))\r\nmyData = data.frame(animal)            # Putting the data in a data frame\r\ntypeof(myData$animal)                  # Checking on our datatype - integer?!\r\nmyData$animal = factor(myData$animal,  # We'll add a new level to the mix\r\n  levels=                              #   with the levels option\r\n    c(levels(myData$animal),           # Combining old levels\r\n     \"hippopotami\"))                   #   with out new entry\r\nmyData$animal[4]                       # Let's look at animal[4]\r\nmyData$animal[4] = \"hippopotami\"       # Now we can add \"hippopotami\" because\r\nmyData$animal[4]                       #   that level is included in the factor\r\n\r\n# Solution 3 for the character\/factor conundrum -- manual conversions ----------\r\nanimal = (c(rep(\"kangas\", 4), rep(\"koalas\", 5)))\r\nmyData = data.frame(animal)            # Putting the data in a data frame\r\ntypeof(myData$animal)                  # Check on the data type\r\nmyData$animal =                        # Force the variable back to character\r\n  as.character(myData$animal)\r\ntypeof(myData$animal)                  # Recheck the type -- that works!\r\nmyData$animal[4]\r\nmyData$animal[4] = \"hippopotami\"       # Now we can make the change to obs 4\r\nmyData$animal[4]\r\n\r\n<\/pre>\n<h3>R Code for Chapter 4: Getting your Data into R<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n#===============================================================================\r\n# Chapter 4-- Getting your data into R\r\n#===============================================================================\r\n# 4.1 Entering data ======================================================== 4.1\r\n\r\n# 4.1a Entering data with the concatenate function ======================== 4.1a\r\n\r\nmyVar = c(1, 7, 3, 5, 9, 21, 8)        # Entering data with concatenate\r\nmyVar                                  # Print data\r\n\r\nmyWords = c(\"ant\", \"ball\", \"clown\")    # Concatenate words into variable\r\nmyWords                                # Print variable\r\n\r\n# 4.1b Joining vectors into matrices and data frames ====================== 4.1b\r\n\r\n# cbind\r\ntemp = c(78.4, 65.3, 72.9, 81.2)       # Setup some data\r\nrain = c(.025, .001, 0, 1.2)\r\nday = c(1, 2, 3, 4)\r\nweather1 = cbind(day, temp, rain)      # cbind demonstration\r\nweather1                               # Show results\r\n\r\n# rbind\r\nday1 = c(.025, 78.4)                   # Setup some data\r\nday2 = c(.001, 65.3)\r\nday3 = c(0, 72.9)\r\nday4 = c(1.2, 81.2)\r\nweather2 = rbind(day1, day2,           # rbind demonstration\r\n  day3, day4)  \r\nweather2                               # Show matrix\r\ncolnames(weather2) = c(\"rain\", \"temp\") # Add variable names for columns\r\nweather2                               # Show weather2 matrix w\/names\r\n\r\n# concatenating different length vectors\r\ntemp = c(78.4, 65.3, 72.9, 81.2)       # Setup some data\r\nrain = c(.025, .001, 0, 1.2)\r\nday = c(\"Saturday\", \"Sunday\")\r\nweekend.weather = data.frame(          # Combine unequal length vectors\r\n  cbind(day, temp, rain))              #   with cbind\r\nweekend.weather                        # Show results\r\n\r\n\r\n# 4.1c Entering data with the R spreadsheet =============================== 4.1c\r\n\r\n#open a spreadsheet and edit a list of data\r\nmyX = c(1, 2, 3)                       # Create variable 1\r\nmyY = c(\"a\", \"b\", \"c\")                 # Create variable 2\r\nmyData = cbind(myX, myY)               # Combine in data frame\r\ndata.entry(myData)                     # Open in data entry spreadsheet\r\n\r\n#open a new data list with a simple dummy value\r\nx = 1\r\ndata.entry(x)\r\n\r\n#use the edit method\r\nmyX = c(1, 2, 3)                       # Create variable 1\r\nmyY = c(\"a\", \"b\", \"c\")                 # Create variable 2\r\nmyData.df = data.frame(myX, myY)       # Combine in data frame\r\nmyData.df = edit(myData.df)            # Use edit() to open spreadsheet\r\n\r\n\r\n# 4.2 Creating data ======================================================== 4.2\r\n\r\n# 4.2a Simple sequence and repetitions ==================================== 4.2a\r\n\r\n# Colon sequences\r\nmyVar1 = 0:10                          # A variable w\/ 0-10 sequence\r\nmyVar1                                 # Show result\r\n\r\nmyVar2 = 10:22                         # A variable w\/ 10-22 sequence\r\nmyVar2                                 # Show result\r\n\r\nmyVar3 = -5:5                          # A variable w\/ -5 to 5 sequence\r\nmyVar3                                 # Show result\r\n\r\n# The sequence function\r\nmyVar4 = seq(0, 5, by = .5)            # A sequence from 0 to 5 by .5\r\nmyVar4                                 # Show result\r\n\r\nmyVar5 = seq(7, 5, -.25)               # A sequence from 7 to 5 by -.25\r\nmyVar5                                 # Show result\r\n\r\n# The repeat function\r\nmyVar1 = rep(7, 3)                     # Create variable w\/ 3 7's\r\nmyVar1                                 # Show result\r\n\r\nmyVar2 = c(rep(1, 5), rep(2, 3))       # Create variable w\/ 5 1's & 3 2's\r\nmyVar2                                 # Show result\r\n\r\nmyVar3 = c(                            # Create variable with\r\n  rep(\"Fred\", 2),                      #   2 \"Fred\" and\r\n  rep(\"Wilma\", 4))                     #   4 \"Wilma\"\r\n\r\nmyVar3                                 # Show result\r\n\r\n# 4.2b Generating factors ================================================= 4.2b\r\n\r\nmyFactor = gl(n = 3,                   # Set up 3 levels\r\n  k = 2,                               # Each repeats twice\r\n  length = 6,                          # Total length is 6\r\n  labels = c(\"small\", \"med\", \"large\")) # Matching labels for 3 levels\r\n\r\nmyFactor                               # Display factor\r\n\r\nmyFactor2 = gl(2, 4, 10, c(\"y\", \"n\"))  # Set 2 levels w\/4 repeats\r\nmyFactor2                              # Display factor\r\n\r\n# 4.2c Random numbers and statistical distributions ======================= 4.2c\r\n\r\n# Values from a uniform distribution\r\nmyRand = runif(10)                     # Generate 10 random numbers between 0,1\r\nmyRand                                 # Print the generated random numbers\r\nround(myRand, 2)                       # Print them again, rounded to 2 places\r\n\r\nmyRand2 = runif(5, min = 0, max = 100) # Generate 5 random values between 1, 100\r\nround(myRand2, 2)                      # Print them\r\n\r\nmyRand3 = runif(3, 0, 10)              # Generate 3 random values between 0, 10\r\nround(myRand3, 2)                      # Print them\r\n\r\n# Values from a normal distribution:\r\n\r\nmyNorm = rnorm(10)                     # 10 values from a standard normal dist.\r\nround(myNorm, 2)                       # Print them\r\n\r\nmyNorm2 =                              # From normal distribution \r\n  rnorm(10, mean = 50, sd = 10)        #   with mean = 50 & std dev = 10 \r\nround(myNorm2, 2)                      # Print them\r\n\r\nmyNorm2 = rnorm(10000)                 # Create 10,000 values from normal dist\r\n\r\npng(filename = \"illustrations\/fig-4-1-normal dist histogram.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\nhist(myNorm2, main = NA)               # Display in histogram\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 4.3 Importing data ======================================================= 4.3\r\n\r\n# 4.3a The working directory ============================================== 4.3a\r\n\r\nsetwd(\"C:\/data\")                       # Set the working directory\r\ngetwd()                                # Show the working directory\r\n\r\nsetwd(dirname(choose.files()))\r\n\r\n# 4.4 The read command: Overview =========================================== 4.4\r\n\r\n# 4.5 Reading from the clipboard =========================================== 4.5\r\n\r\nmyData = read.delim(\"clipboard\", header = TRUE)\r\n\r\n# 4.6 Reading blank delimited data ========================================= 4.6\r\n\r\nmyData = read.table(\"myDataFile.txt\", header = FALSE)\r\n\r\n# 4.7 Reading comma separated values ======================================= 4.7\r\n\r\nmyData = read.csv(\"myDataFile.csv\", header = TRUE)\r\n\r\n# 4.8 Reading tab separated values ========================================= 4.8\r\n\r\nmyData = read.delim(\"myDataFile.txt\", header = TRUE)\r\n\r\n# 4.9 Fixed width data ===================================================== 4.9\r\n\r\nmyData = read.fwf(filename, width = c(5, 2, 3, 2, 15, 3, 2))\r\n\r\n# Reading multiline data\r\n# We set up two vectors with the relevant width instructions for each line\r\nline1 = c(3, -2, 5, 2, 3, 2, 15)       # We'll skip the line numbers with the -2\r\nline2 = c(-3, -2, 3, 2)                # Skip obs & line numbers on 2nd line\r\nmyData = read.fwf(\"myDataFile.txt\", width = list(line1, line2))\r\n\r\n# 4.10 Generic tabular data =============================================== 4.10\r\n\r\n# Three different kinds of value separators:\r\nmyData = read.table(\"myDataFile.txt\", header = TRUE, sep =  \",\")\r\nmyData = read.table(\"myDataFile.txt\", header = TRUE, sep =  \"\/t\")\r\nmyData = read.table(\"myDataFile.txt\", header = TRUE, sep =  \"%\")\r\n\r\n# 4.11 Importing foreign filetypes ======================================== 4.11\r\n\r\nlibrary(foreign)\r\nmyData = read.dta(\"c:\/data\/mydata.dta\")\r\n\r\n# 4.11a Exporting data in foreign formats ================================ 4.11a\r\n\r\n# 4.12 Integrating SQL with R ============================================= 4.12\r\n\r\n# 4.13 Extracting data from complex datasources =========================== 4.13\r\n\r\n# 4.14 Web scraping ======================================================= 4.14\r\n\r\n# 4.15 Dealing with multi-dimensional tables ============================== 4.15\r\n\r\n# 4.16 Importing problematic charactters ================================== 4.16\r\n\r\n# Strip white example - for removing extra leading or trailing spaces\r\nmyData = read.delim(\"myDataFile.txt\",  # Read some data\r\n  sep = \"\\t\",                          # sep = tab \r\n  strip.white = T,                     # remove spaces\r\n  header = T)                          # data has headers\r\n\r\n\r\n# 4.17 More resources ===================================================== 4.17\r\n\r\n<\/pre>\n<h3>R Code for Chapter 5: Reviewing and Summarizing Data<\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n#===============================================================================\r\n# Chapter 5 -- Reviewing and summarizing data\r\n#===============================================================================\r\n\r\n# Set up some synthetic data to use in chapters five and six\r\n# NOTE: This assumes that the stringsAsFactors option is turned on (the default)\r\n# If stringsAsFactors is turned off, you'll want to enclose the animal variable\r\n# in the as.factor() function:  animal = as.factor(c(rep(\"Mouse\",25...)))\r\n\r\nch5data = data.frame(                  # Create a data frame\r\n  year = c(1935:1959,                  # First variable is years\r\n    1940:1949,                         # Sequence with colon \r\n    1935, 1942, 1958, 1964, 1970),     # Some more years with commas\r\n  animal = c(rep(\"Mouse\", 25),         # Next variable is animal type\r\n    rep(\"Kangaroo\", 10),               # 25 mice, 10 Kangaroos\r\n    rep(\"Elephant\", 5)),               #    5 Elephants\r\n  weight = round(c(                    # Next is plausible weights\r\n    rnorm(25, mean = .06, sd = .006),  #   normally distributed \r\n    rnorm(10, mean = 100, sd = 14),    #   around a mean \r\n    rnorm(5, mean = 12000,             #   and std. dev\r\n      sd = 700)), 3),                  #   and rounded to 3 digits\r\n  captive = rep(                       # Finally a captivity status from\r\n    c(TRUE, FALSE, FALSE, FALSE), 10)) #   a repeated TRUE\/FALSE pattern\r\n\r\n# Some overview ----------------------------------------------------------------\r\nattributes(ch5data)                    # Show data frame attributes\r\nstr(ch5data)                           # Show data frame structure\r\n\r\n# data frame dimensions\r\nnames(ch5data)                         # Show variable names in the data frame\r\ndim(ch5data)                           # Show the dimensions of the data frame\r\nnrow(ch5data)                          # Show the number of rows\r\ndim(ch5data)[1]                        # Show the number of rows\r\nncol(ch5data)                          # Show the number of columns\r\ndim(ch5data)[2]                        # Show the number of columns\r\n\r\n# 5.1 Summarizing Data ===================================================== 5.1\r\n\r\nsummary(ch5data)                       # Summary statistics for ch5data\r\n\r\n# Individual summary statistics\r\nlength(ch5data$animals)                # Show num of obs in animals variable\r\nlength(ch5data)                        # Show num of variables in ch5data\r\nmean(ch5data$year)                     # Show mean of year\r\nmedian(ch5data$weight)                 # Show median of weight\r\nsum(ch5data$weight)                    # Show sum of variable weight\r\nmax(ch5data$year)                      # Show maximum value of year\r\nmin(ch5data$year)                      # Show minimum value of year\r\nrange(ch5data$year)                    # Show range of variable x3\r\nsd(ch5data$year)                       # Show standard deviation of year\r\nunique(ch5data$year)                   # Show unique values in year\r\n\r\n# missing values ---------------------------------------------------------------\r\n\r\nmyVar = c(1, 2, 3, 4, NA, 5)           # A variable w\/ missing values\r\nmean(myVar)                            # Mean function returns NA\r\nmean(myVar, na.rm = T)                 # Mean without missing values\r\n\r\n# 5.2 Sampling  ============================================================ 5.2\r\n\r\n# Displaying the top of the dataset\r\nhead(ch5data, 7)                       # Display first 7 observations\r\n\r\n# Sampling for a simple vector\r\nmyVector = c(1:1000)                   # Create a vector\r\nsample(myVector, 5)                    # A random sample of 5 observations\r\n\r\n# Now for a data frame\r\n\r\nsample5 = sample(1:nrow(ch5data), 5)   # Create a vector of 5 sample values\r\n   \r\nch5data[sample5,]                      # Print sample of data\r\nch5data[sample(1:nrow(ch5data), 5),]   # All in one line\r\n                                       \r\n# 5.3 Reviewing Data by Categories     ===================================== 5.3\r\n                                      \r\ntable(ch5data$animal, ch5data$captive) # Create animal x captive table\r\ntable(ch5data$captive, ch5data$animal) # Create captive x animal table\r\n\r\n# The crosstabs approach (see appendix I) --------------------------------------\r\nxtabs(\r\n   ch5data, formula =                  # Use formula =  to set up dimensions\r\n   ~ animal + captive)\r\n\r\nmy.xtab = xtabs(                       # Save the crosstab for later analysis\r\n   ch5data,formula = \r\n   ~ animal + captive)\r\n\r\nsummary(my.xtab)                       # Generate Chi-Square statistic for x-tab\r\n\r\n# Using the split() function ---------------------------------------------------\r\n\r\nlapply(split(ch5data, ch5data$captive), summary)\r\nlapply(split(ch5data, ch5data$animal), summary)\r\nlapply(split(ch5data, as.list(ch5data$captive, ch5data$animal)), summary)\r\nlapply(split(ch5data, ch5data$captive), summary)\r\n\r\nlapply(split(ch5data$weight, ch5data$animal), mean)\r\nlapply(split(ch5data, ch5data$animal), median, na.rm = T)\r\n\r\n# The cut() function -----------------------------------------------------------\r\nlapply(                                # Apply a function to a data frame\r\n  split(ch5data,                       # Split up data before applying \r\n    cut(ch5data$weight,                # Variable to use as factor\r\n      breaks = c(0, 50, 500, 100000),  # Break points (including ends)\r\n      labels =                         # Labels for the different levels\r\n        c(\"small\", \"large\", \"huge\"))),   \r\n  summary)                             # Function to apply\r\n\r\n\r\n# 5.4 Histograms =========================================================== 5.4\r\n\r\nmyVar = seq(-3, 3, .0001)              # Set up a variable with .0001 intervals\r\nmyNormVar = rnorm(myVar)               # Create random normal variable on myVar\r\n\r\npng(filename = \"illustrations\/fig-5-1-histogram.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\nhist(myNormVar, main = NA)             # Histogram plot of normal variable\r\n                                       #  with title (main = ) turned off\r\ndev.off()\r\n                                       # Output png plot\r\n# Kernel Density Plot\r\n\r\npng(filename = \"illustrations\/fig-5-2-kernel density.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n                   \r\nplot(density(myNormVar), main = NA)    # Density plot of normal variable\r\npolygon(density(myNormVar), \r\n  col = \"gray\")                        # Fill it in with gray if you want\r\n\r\ndev.off()                              # Output png file\r\n\r\n# 5.5 Scatterplots                     ===================================== 5.5\r\n\r\nmyX = c(1, 5, 19, 7, 6, 18, 11, 10)    # Create an x variable\r\nmyY = c(4, 3, 12, 7, 8, 9, 15, 9)      # Create a y variable\r\n\r\npng(filename = \"illustrations\/fig-5-3-basic scatterplot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\nplot(myX, myY)                         # Simple scatterplot of x & y\r\n\r\ndev.off()                              # Output png file\r\n\r\n# adding labels\r\n\r\nmyLabels = c(                          # Add a vector of labels for each obs\r\n  \"Bach\", \"Beethoven\",\r\n  \"Brahms\", \"Mozart\", \"Chopin\",\r\n  \"Tchaikovsky\", \"Satie\", \"Bartok\")\r\n\r\npng(filename = \"illustrations\/fig-5-4-scatterplot labels.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\nplot(myX, myY)                         # Simple scatterplot of x & y\r\ntext(myX, myY,                         # Add labels using x & y coords\r\n  labels = myLabels,                   # The labels to add\r\n  pos = 3,                             # Put labels above points\r\n  xpd = TRUE)                          # Allow printing outside plot\r\n\r\ndev.off()                              # Output png file\r\n\r\n# Only add labels to more extreme points (not in book)\r\n\r\nmyData = data.frame(myX, myY, myLabels)# Package data in data frame\r\nplot(myData$myX, myData$myY)           # Scatterplot of myX & myY\r\n\r\nmyData2 = myData[                      # Select cases where \r\n  abs(myData$myY - mean(myData$myY)) > #   deviation of Y from mean >\r\n  sd(myData$myY) &                     #   1 std dev and where\r\n  abs((myData$myY - mean(myData$myY))\/ #   standardized deviation of Y\r\n  sd(myData$myY)) > 2 *                #   is greater than 2 times the    \r\n  abs((myData$myX - mean(myData$myY))\/ #   standardized deviation of X\r\n  sd(myData$myX)),]\r\n\r\ntext(myData2$myX, myData2$myY,         # Use coordinates of extreme points\r\n  myData2$myLabels,                    #   to place labels\r\n  pos = 4)                             #   to the right of the points\r\n\r\n# 5.6 Pairs Plots ========================================================== 5.6\r\n\r\nmyZ = c(3, 9, 12, 2, 2, 17, 1, 8)      # Add z variable to x & y from sect 5.5\r\nmyData = data.frame(myX, myY, myZ)     # Combine x,y,z into data frame \r\n\r\npng(filename = \"illustrations\/fig-5-5-pairs plot.png\",\r\n  units = \"in\",                        # Set measurements in inches\r\n  res = 1200,                          # Set resolution at 1200dpi\r\n  width = 6,                           # Width at 6 inches\r\n  height = 4)                          # Height at 4 inches\r\n\r\npar(mai = c(1, 1, .25, .25))           # Set margins - no Title\r\n\r\npairs(myData)                          # Pairs plot for data frame\r\n\r\ndev.off()                              # Output png file\r\n\r\n<\/pre>\n<h3>R Code for Chapter 6: Sorting and Selecting Data <\/h3>\n<pre lang=\"r-script\" line=\"1\">\r\n#===============================================================================\r\n# Chapter 6 -- Sorting and selecting data\r\n#===============================================================================\r\n\r\n# We'll use the same data we set up at the beginning of chapter 5 throughout\r\n# this chapter\r\n\r\n# 6.1 Using index values for selection ===================================== 6.1\r\n\r\nselection = c(1, 5, 7, 11:14)          # Set a vector of rows to select\r\nmyDF = ch5data[selection,]             # Create data frame w\/selected rows\r\nmyDF = ch5data[c(1, 5, 7, 11:14),]     # Create data frame w\/selected rows\r\n\r\nmyDF                                   # Print myDF\r\nmyDF[-2,]                              # Print myDF w\/o observation 2\r\n\r\nmyDF2 = myDF[,-c(1, 3)]                # Drop columns 1 & 3\r\n\r\nmyDF4 = myDF[-c(1, 3, 7),]             # Drop rows 1,3 & 7\r\n\r\n# 6.2 Selecting with conditional values===================================== 6.2\r\n\r\nmyDF = ch5data                         # A shorter DF name for convenience\r\nkDF = myDF[myDF$animal == \"Kangaroo\",] # Select all kangaroos\r\nhead(kDF,3)                            # First 3 kangaroo observations\r\n\r\nbigkDF =                               # New data frame for big kangaroos\r\n  myDF[myDF$animal == \"Kangaroo\" &     # Select all kangaroos\r\n  myDF$weight>100,]                    #   weighing more than 100 lbs\r\n\r\n# The which() approach ---------------------------------------------------------\r\nkselect = which(myDF$weight>100 &      # Create a vector of obs numbers\r\n  myDF$animal == \"Kangaroo\")           #   with Kangaroos > 165 lbs\r\nkselect                                # Print selection vector\r\nbigkDF2 = myDF[kselect,]               # Create data frame w\/selected\r\nbigkDF2                                # Print data frame\r\n\r\n# 6.3 Using subset() ======================================================= 6.3\r\n\r\nmyData = data.frame(\r\n  myV1 = c(10, 20, 30, 40),            # Set up 3 variables in a data frame\r\n  myV2 = c(\"a\", \"b\", \"c\", \"d\"),\r\n  myV3 = c(1, 2, 3, 4))\r\nmyData                                 # Display the data frame\r\n\r\n# Subset by rows\r\nmyData1 = subset(myData, myV1>20)      # Select rows with myV1>20\r\nmyData1                                # Display results\r\nmyData2 = myData[myV1>20,]             # Same selection with bracket method\r\nmyData2                                # Display results\r\n                                                                  \r\n# Subset by columns                     \r\nmyData3 = subset(myData,               # Create a data subset\r\n  select = myV1)                       #    with just myV1\r\nmyData3                                # Display the result\r\nmyData4 = subset(myData,               # Create a data subset\r\n  select = -myV3)                      #   without myV3\r\nmyData4                                # Display the result\r\n\r\nmyData3b = myData[,1]                  # Same result w\/bracket approach\r\nmyData3b                               # Display result\r\nmyData4b = myData[,-3]                 # Same result w\/bracket approach\r\nmyData4b                               # Display result\r\nmyData3c = myData[,\"myV1\"]             # Using variable name to select col\r\nmyData3c                               # Display result\r\nmyData4c = myData[,c(\"myV1\", \"myV2\")]  # Use variable name for col selection\r\nmyData4c                               # Display result\r\n\r\n\r\n# Subsetting with a vector\r\nmyData5 = subset(                      # Create a subset with just\r\n  myData, select = c(myV1, myV3))      #   myV1 and myV3 \r\nmyData5                                # Display the result\r\n\r\nmyData6 = subset(                      # Create a subset without\r\n  myData, select = -c(myV2, myV3))     #   myV2 and myV3\r\nmyData6                                # Display the result\r\n\r\nmySelector = c(\"myV1\", \"myV3\")         # Create vector to select variables\r\nmyData7 = subset(                      # Subset the data using\r\n  myData, select = mySelector)         #   the mySelector vector\r\nmyData7                                # Display the result\r\n\r\n# 6.4 Splitting data into groups with by() ================================= 6.4\r\n\r\nmyDF = ch5data                         # Make sure the data is reset\r\n\r\nsummary(myDF)                          # Summary of the whole ch 5 dataset\r\nby(myDF, myDF$animal, summary)         # Summary by animal type\r\nby(myDF,                               # \r\n  interaction(myDF$animal,             # Interact two factors:  animal type\r\n    myDF$captive),                     #   and captivity\r\n  summary)                             # Apply the summary function\r\n\r\n\r\n#6.5 Splitting up continuous numeric data ================================== 6.5\r\n\r\n# Splitting data with cut() ----------------------------------------------------\r\nmyVar = rnorm(100)                     # Create var from std. normal dist\r\nmyVar2 = cut(myVar, -3:3)              # Cut into 6 1 unit segments\r\ntable(myVar2)                          # Show number of obs in each segment\r\n\r\n# Supress labels ---------------------------------------------------------------\r\nmyVar2 = cut(myVar, -3:3,              # Split up myVar \r\n  labels = FALSE)                      #   without labels\r\ntable(myVar2)                          # Show number of obs in each segment\r\n\r\n# Using quantile() -------------------------------------------------------------\r\nmyVar = rnorm(n = 100)                 # Set up data drawn from normal dist\r\nquantile(myVar)                        # Default quantiles is quartiles\r\nquantile(myVar,                        # Set up quintiles manually\r\n  probs = (c(0, .2, .4, .6, .8, 1))) \r\nquantile(myVar,                        # Set up quintiles with seq()\r\n  probs = seq(0, 1, by = .2))  \r\nquantile(myVar,                        # Some custom cuts for normal curve\r\n  probs = c(.0001, .01, .025, .5, .975, .99, .9999))\r\n\r\n# cut and quantile together ----------------------------------------------------\r\nmyVar = rnorm(n = 100)                 # Set up data drawn from normal dist\r\nmyVarQ = cut(myVar,                    # Divide myVar into bins\r\n  quantile(myVar),                     # Use 4 bins w\/equal num of obs (default)\r\n  include.lowest = T)                  # Include lowest value in 1st bin\r\n   \r\nsummary(myVarQ)                        # Show quartiles\r\nhead(cbind(myVar, myVarQ))             # Show first 6 observations\r\n\r\nmyVarQ2 = cut(myVar,                   # Divide myVar into bins\r\n  quantile(myVar,                      # Use 5 bins w\/equal num of obs\r\n    prob = seq(0, 1, by = .2)),  \r\n  include.lowest = T)                  # Include lowest value in 1st bin\r\n   \r\nsummary(myVarQ2)                       # Show quintiles\r\nhead(cbind(myVar, myVarQ2))            # Show first 6 observations\r\n\r\n\r\n# 6.6 Sorting and Ordering Data ============================================ 6.6\r\n\r\n# 6.6a Sorting a variable ================================================= 6.6a\r\n# Sorting example --------------------------------------------------------------\r\n\r\nnumbers = c(1, 6, 5, 7, 8)             # Some numbers to sort\r\nsort(numbers)                          # Voila!  the numbers sorted\r\n\r\n# Ordering example -------------------------------------------------------------\r\nfruit = c(\"Apple\", \"banana\",           # Some fruit data\r\n  \"apple\", \"Banana\")\r\nfruit = fruit[order(fruit)]            # Ordered by fruit\r\nfruit                                  # Display in order\r\n\r\n\r\n# 6.6b Ordering a data frame ============================================== 6.6b\r\n\r\n# Show the difference between order() and sort() -------------------------------\r\nnumbers = c(1, 6, 5, 7, 8)             # Some numbers to sort\r\nsort(numbers)                          # Sorted --> order of numbers\r\norder(numbers)                         # Ordered --> obs nums of sorted obs\r\n\r\n# A data frame ordering example ------------------------------------------------\r\n\r\nmyDF2 = subset(ch5data,                # For tractibility in example\r\n  year>1940 & year<1945)               #   take subset of myDF\r\nmyDF2 = myDF2[order(myDF2$year),]      # Sort myDF2 by year\r\nmyDF2                                  # Display result\r\n\r\n# Ordering by two variables ----------------------------------------------------\r\n\r\nmyDF2 = myDF2[order(myDF2$year,        # Order first by year\r\n  myDF2$animal),]                      #   and then by animal name\r\nmyDF2                                  # Show result\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>R Code for Chapter 1: Introduction #=============================================================================== # Chapter 1 &#8212; Introduction #=============================================================================== # 1.1a &#8211; R commands ======================================================= 1.1a # This comment identifies the purpose of this R-script # It is really just to demonstrate what R commands look like. # R Doesn&#8217;t care about blank lines. My first command will create an # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":98,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-102","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":11,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/102\/revisions"}],"predecessor-version":[{"id":524,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/102\/revisions\/524"}],"up":[{"embeddable":true,"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/pages\/98"}],"wp:attachment":[{"href":"http:\/\/kktg.net\/sgr\/wp-json\/wp\/v2\/media?parent=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}