Don't Hand This One In
Total Page:16
File Type:pdf, Size:1020Kb
Lab 9 Don't hand this one in 1. Male tree crickets produce mating songs by rubbing their wings together to produce a chirping sound. It is hypothesized that female tree crickets identify males of the correct species by how fast (in chirps per second) the male's mating song is. This is called the pulse rate. Some data for two species of crick- ets are in http://www.utsc.utoronto.ca/~butler/c32/crickets.txt. The columns (unlabelled) are temperature and pulse rate (respectively) for Oecanthus exclamationis (rst two columns) and Oecan- thus niveus (third and fourth columns). There are some missing values in the rst two columns because fewer exclamationis crickets than niveus crickets were measured. The research question is whether males of the dierent species have dierent average pulse rates. It is also of interest to see whether temperature has an eect, and if so, what. (a) Read in the data with header=F. You'll see that the columns have names V1 through V4. This is OK. Solution: R> crickets=read.table("crickets.txt",header=F) R> crickets V1 V2 V3 V4 1 20.8 67.9 17.2 44.3 2 20.8 65.1 18.3 47.2 3 24.0 77.3 18.3 47.6 4 24.0 78.7 18.3 49.6 5 24.0 79.4 18.9 50.3 6 24.0 80.4 18.9 51.8 7 26.2 85.8 20.4 60.0 8 26.2 86.6 21.0 58.5 9 26.2 87.5 21.0 58.9 10 26.2 89.1 22.1 60.7 11 28.4 98.6 23.5 69.8 12 29.0 100.8 24.2 70.9 13 30.4 99.3 25.9 76.2 14 30.4 101.7 26.5 76.1 15 NA NA 26.5 77.0 16 NA NA 26.5 77.7 17 NA NA 28.6 84.7 As promised. (b) These data are rather far from being tidy. There need to be three variables, temperature, pulse rate and species, and there are 14 + 17 = 31 observations altogether. This one is tricky in that there are temperature and pulse rate for each of two levels of a factor, so I'll suggest combining the temperature and chirp rate together into one thing for each species, then gathering them, then 1 splitting them again. Load the libraries dplyr and tidyr, then create new columns, named for each species, that contain the temperature and pulse rate for that species in that order, pasted together. Solution: Breathe, and then begin. mutate creates new columns. R> library(tidyr) R> library(dplyr) R> crickets %>% mutate(exclamationis=paste(V1,V2)) %>% R> mutate(niveus=paste(V3,V4)) -> crickets.1 R> crickets.1 Attaching package: `dplyr' The following objects are masked from `package:stats': filter, lag The following objects are masked from `package:base': intersect, setdiff, setequal, union V1 V2 V3 V4 exclamationis niveus 1 20.8 67.9 17.2 44.3 20.8 67.9 17.2 44.3 2 20.8 65.1 18.3 47.2 20.8 65.1 18.3 47.2 3 24.0 77.3 18.3 47.6 24 77.3 18.3 47.6 4 24.0 78.7 18.3 49.6 24 78.7 18.3 49.6 5 24.0 79.4 18.9 50.3 24 79.4 18.9 50.3 6 24.0 80.4 18.9 51.8 24 80.4 18.9 51.8 7 26.2 85.8 20.4 60.0 26.2 85.8 20.4 60 8 26.2 86.6 21.0 58.5 26.2 86.6 21 58.5 9 26.2 87.5 21.0 58.9 26.2 87.5 21 58.9 10 26.2 89.1 22.1 60.7 26.2 89.1 22.1 60.7 11 28.4 98.6 23.5 69.8 28.4 98.6 23.5 69.8 12 29.0 100.8 24.2 70.9 29 100.8 24.2 70.9 13 30.4 99.3 25.9 76.2 30.4 99.3 25.9 76.2 14 30.4 101.7 26.5 76.1 30.4 101.7 26.5 76.1 15 NA NA 26.5 77.0 NA NA 26.5 77 16 NA NA 26.5 77.7 NA NA 26.5 77.7 17 NA NA 28.6 84.7 NA NA 28.6 84.7 (c) The two columns exclamationis and niveus that you just created are both temperature-pulse rate combos, but for dierent species. gather them together into one column, labelled by species. (This is a straight tidyr gather, even though they contain something odd-looking.) Solution: Thus, this: R> crickets.2=gather(crickets.1,species,temp.pulse,exclamationis:niveus) R> crickets.2 V1 V2 V3 V4 species temp.pulse 1 20.8 67.9 17.2 44.3 exclamationis 20.8 67.9 2 20.8 65.1 18.3 47.2 exclamationis 20.8 65.1 Page 2 3 24.0 77.3 18.3 47.6 exclamationis 24 77.3 4 24.0 78.7 18.3 49.6 exclamationis 24 78.7 5 24.0 79.4 18.9 50.3 exclamationis 24 79.4 6 24.0 80.4 18.9 51.8 exclamationis 24 80.4 7 26.2 85.8 20.4 60.0 exclamationis 26.2 85.8 8 26.2 86.6 21.0 58.5 exclamationis 26.2 86.6 9 26.2 87.5 21.0 58.9 exclamationis 26.2 87.5 10 26.2 89.1 22.1 60.7 exclamationis 26.2 89.1 11 28.4 98.6 23.5 69.8 exclamationis 28.4 98.6 12 29.0 100.8 24.2 70.9 exclamationis 29 100.8 13 30.4 99.3 25.9 76.2 exclamationis 30.4 99.3 14 30.4 101.7 26.5 76.1 exclamationis 30.4 101.7 15 NA NA 26.5 77.0 exclamationis NA NA 16 NA NA 26.5 77.7 exclamationis NA NA 17 NA NA 28.6 84.7 exclamationis NA NA 18 20.8 67.9 17.2 44.3 niveus 17.2 44.3 19 20.8 65.1 18.3 47.2 niveus 18.3 47.2 20 24.0 77.3 18.3 47.6 niveus 18.3 47.6 21 24.0 78.7 18.3 49.6 niveus 18.3 49.6 22 24.0 79.4 18.9 50.3 niveus 18.9 50.3 23 24.0 80.4 18.9 51.8 niveus 18.9 51.8 24 26.2 85.8 20.4 60.0 niveus 20.4 60 25 26.2 86.6 21.0 58.5 niveus 21 58.5 26 26.2 87.5 21.0 58.9 niveus 21 58.9 27 26.2 89.1 22.1 60.7 niveus 22.1 60.7 28 28.4 98.6 23.5 69.8 niveus 23.5 69.8 29 29.0 100.8 24.2 70.9 niveus 24.2 70.9 30 30.4 99.3 25.9 76.2 niveus 25.9 76.2 31 30.4 101.7 26.5 76.1 niveus 26.5 76.1 32 NA NA 26.5 77.0 niveus 26.5 77 33 NA NA 26.5 77.7 niveus 26.5 77.7 34 NA NA 28.6 84.7 niveus 28.6 84.7 (d) Now split up the temperature-pulse combos at the space, into two separate columns. This is separate. When specifying what to separate by, you can use a number (split after this many characters) or a piece of text, in quotes (when you see this text, split at it). Solution: The text to split by is a space (in quotes), since paste puts a space in between the values it pastes together: R> crickets.3=separate(crickets.2,temp.pulse,c("temperature","pulse.rate")," ") R> crickets.3 R> str(crickets.3) V1 V2 V3 V4 species temperature pulse.rate 1 20.8 67.9 17.2 44.3 exclamationis 20.8 67.9 2 20.8 65.1 18.3 47.2 exclamationis 20.8 65.1 3 24.0 77.3 18.3 47.6 exclamationis 24 77.3 4 24.0 78.7 18.3 49.6 exclamationis 24 78.7 5 24.0 79.4 18.9 50.3 exclamationis 24 79.4 6 24.0 80.4 18.9 51.8 exclamationis 24 80.4 7 26.2 85.8 20.4 60.0 exclamationis 26.2 85.8 Page 3 8 26.2 86.6 21.0 58.5 exclamationis 26.2 86.6 9 26.2 87.5 21.0 58.9 exclamationis 26.2 87.5 10 26.2 89.1 22.1 60.7 exclamationis 26.2 89.1 11 28.4 98.6 23.5 69.8 exclamationis 28.4 98.6 12 29.0 100.8 24.2 70.9 exclamationis 29 100.8 13 30.4 99.3 25.9 76.2 exclamationis 30.4 99.3 14 30.4 101.7 26.5 76.1 exclamationis 30.4 101.7 15 NA NA 26.5 77.0 exclamationis NA NA 16 NA NA 26.5 77.7 exclamationis NA NA 17 NA NA 28.6 84.7 exclamationis NA NA 18 20.8 67.9 17.2 44.3 niveus 17.2 44.3 19 20.8 65.1 18.3 47.2 niveus 18.3 47.2 20 24.0 77.3 18.3 47.6 niveus 18.3 47.6 21 24.0 78.7 18.3 49.6 niveus 18.3 49.6 22 24.0 79.4 18.9 50.3 niveus 18.9 50.3 23 24.0 80.4 18.9 51.8 niveus 18.9 51.8 24 26.2 85.8 20.4 60.0 niveus 20.4 60 25 26.2 86.6 21.0 58.5 niveus 21 58.5 26 26.2 87.5 21.0 58.9 niveus 21 58.9 27 26.2 89.1 22.1 60.7 niveus 22.1 60.7 28 28.4 98.6 23.5 69.8 niveus 23.5 69.8 29 29.0 100.8 24.2 70.9 niveus 24.2 70.9 30 30.4 99.3 25.9 76.2 niveus 25.9 76.2 31 30.4 101.7 26.5 76.1 niveus 26.5 76.1 32 NA NA 26.5 77.0 niveus 26.5 77 33 NA NA 26.5 77.7 niveus 26.5 77.7 34 NA NA 28.6 84.7 niveus 28.6 84.7 'data.frame': 34 obs.