--- title: "R Notebook" output: html_notebook --- This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*. ```{r} plot(cars) ``` Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*. When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file). ```{r} 2+3 2*3*4 sqrt(10) pi * pi ``` plot() is equivalent to a graphical plot c() creates a list by concatenating the numbers into a single vector object ```{r} plot(c(1,3,5,6)) ``` plot() with 2 lists is equivalent to a "scatter plot"" ```{r} plot(c(1,3,5,6),c(2,4,6,3)) ``` Commands: help(), apropos(), help.search() ```{r} help(plot) apropos("sort") help.search("sort") ``` for each amount that an elastic band is stretched, we measure the distance traveled by the band when released. ```{r} elasticBand <- data.frame(stretch = c(46, 54, 48, 50, 44, 42, 52), distance = c(148, 182, 173, 166, 109, 141, 166)) # print(elasticBand) summary(elasticBand) ``` how to quit ```{r} rm(elasticBand) rm(C) rm(F) # quit() ``` converting C to F ```{r} Ce <- 25:30 Fa <- 9/5*Ce+32 C2F <- data.frame(Celsius=Ce, Fahrenheit=Fa) print(C2F) ``` VECTORS ```{r} c(2,6,5) ``` ```{r} 3:10 ``` ```{r} X <- c(T,F,T,T) X ``` ```{r} Y <- c("Alice", "Bob", "Cathy") Y ``` ```{r} Z <- c(X,Y) Z ``` missing data ```{r} X <- c(1, NA, 3, 0, NA) X ``` transforming missing data ```{r} X[is.na(X)] <- 0 X ``` First install DAAG - a collection of data for our experiments ```{r} library(DAAG) # load DAAG package data(Cars93.summary) # copy Cars93.summary into workspace Cars93.summary ``` ```{r} data(airquality) # names(airquality) # variables in this data frame summary(airquality) # airquality sapply(airquality, mean) # apply to each column of data frame ``` ```{r} # plotting a scatter plot attach(airquality) # applies the next plot command to this data frame plot(Ozone, Wind) detach(airquality) ``` ```{r} # plotting a scatter plot attach(airquality) # applies the next plot command to this data frame plot(Ozone, Wind) detach(airquality) ``` ```{r} data(possum) # copy possum into workspace fossum <- possum[possum$sex == "f", ] # pick the females par(mfrow = c(1,2)) attach(fossum) hist(totlngth, breaks = 72.5 + (0:5) * 5, ylim = c(0, 22), xlab = "Total length", main = "A: Breaks at 72.5, 77.5, ...") hist(totlngth, breaks = 75 + (0:5) * 5, ylim = c(0, 22), xlab = "Total length", main = "B: Breaks at 75, 80, ...") ``` ```{r} dens <-density(totlngth) xlim <-range(dens$x); ylim <- range(dens$y) par(mfrow = c(1,2)) hist(totlngth, breaks = 72.5 + (0:5) * 5, probability = T, xlim = xlim, ylim = ylim, xlab = "Total length", main = "C: Breaks at 72.5, 77.5, ...") lines(dens) hist(totlngth, breaks = 75 + (0:5) * 5, probability = T, xlim = xlim, ylim = ylim, xlab = "Total length", main = "D: Breaks at 75, 80, ...") lines(dens) # detach(fossum) ```