Day 4 R questions


At the end of class, Jan 16, I asked what questions you had. Here are some questions and answers.
  1. What does rm remove? We had a diamonds dataset with a column called carat. I then created a copy of carat by changing its first element to 45.
    diamonds <- read.table("http://www.amstat.org/publications/jse/datasets/4c.dat")
     names(diamonds) <-  c("carat","color","clarity", "cert", "price")
      summary(diamonds$carat)
      attach(diamonds)
      summary(carat)  ## same as above. attach lets us drop the diamonds$
      carat[1] <- 45  ## creates a new copy of carat in the local workspace
      summary(carat)  ## finds the new copy first, so max is 45
      rm(carat)       ## removes the new copy from local workspace
      summary(carat)  ## finds the carat column in the attached dataset,  max = 1.1
    
    rm removes things from the local workspace, not from an attached dataset. Similarly, changing something after attaching creates a new copy and does not modify the original.
  2. How do I plot with different colors and symbols?
    For colors, add col=group (after a comma) inside the plot command, where group is one of the variables in your dataframe. It could be a factor or numeric column. To see what colors will be used, look at the palette function, but the defaults are black (1), red(2), green(3), blue(4), cyan, magenta, .... To change shapes, use pch = group, where now group must be numeric. Example:
     plot(x = 1:20, y = 1:20, col = 1:20, pch = 1:20)