Due by end of day on 27 Jan 2021 Email your .R file to brian.beckage@uvm.edu with name as ‘pbio394_yourLastName_ex1.R’ and email header ‘pbio394 yourLastName Ex1’.
Below are a series of short exercises to become familiar with the basics of R.
myV<-seq(from=1,to=100,len=200)
myV
## [1] 1.000000 1.497487 1.994975 2.492462 2.989950 3.487437
## [7] 3.984925 4.482412 4.979899 5.477387 5.974874 6.472362
## [13] 6.969849 7.467337 7.964824 8.462312 8.959799 9.457286
## [19] 9.954774 10.452261 10.949749 11.447236 11.944724 12.442211
## [25] 12.939698 13.437186 13.934673 14.432161 14.929648 15.427136
## [31] 15.924623 16.422111 16.919598 17.417085 17.914573 18.412060
## [37] 18.909548 19.407035 19.904523 20.402010 20.899497 21.396985
## [43] 21.894472 22.391960 22.889447 23.386935 23.884422 24.381910
## [49] 24.879397 25.376884 25.874372 26.371859 26.869347 27.366834
## [55] 27.864322 28.361809 28.859296 29.356784 29.854271 30.351759
## [61] 30.849246 31.346734 31.844221 32.341709 32.839196 33.336683
## [67] 33.834171 34.331658 34.829146 35.326633 35.824121 36.321608
## [73] 36.819095 37.316583 37.814070 38.311558 38.809045 39.306533
## [79] 39.804020 40.301508 40.798995 41.296482 41.793970 42.291457
## [85] 42.788945 43.286432 43.783920 44.281407 44.778894 45.276382
## [91] 45.773869 46.271357 46.768844 47.266332 47.763819 48.261307
## [97] 48.758794 49.256281 49.753769 50.251256 50.748744 51.246231
## [103] 51.743719 52.241206 52.738693 53.236181 53.733668 54.231156
## [109] 54.728643 55.226131 55.723618 56.221106 56.718593 57.216080
## [115] 57.713568 58.211055 58.708543 59.206030 59.703518 60.201005
## [121] 60.698492 61.195980 61.693467 62.190955 62.688442 63.185930
## [127] 63.683417 64.180905 64.678392 65.175879 65.673367 66.170854
## [133] 66.668342 67.165829 67.663317 68.160804 68.658291 69.155779
## [139] 69.653266 70.150754 70.648241 71.145729 71.643216 72.140704
## [145] 72.638191 73.135678 73.633166 74.130653 74.628141 75.125628
## [151] 75.623116 76.120603 76.618090 77.115578 77.613065 78.110553
## [157] 78.608040 79.105528 79.603015 80.100503 80.597990 81.095477
## [163] 81.592965 82.090452 82.587940 83.085427 83.582915 84.080402
## [169] 84.577889 85.075377 85.572864 86.070352 86.567839 87.065327
## [175] 87.562814 88.060302 88.557789 89.055276 89.552764 90.050251
## [181] 90.547739 91.045226 91.542714 92.040201 92.537688 93.035176
## [187] 93.532663 94.030151 94.527638 95.025126 95.522613 96.020101
## [193] 96.517588 97.015075 97.512563 98.010050 98.507538 99.005025
## [199] 99.502513 100.000000
sum(myV*2)
## [1] 20200
sum(myV^2)
## [1] 675041.7
myV<-seq(from=1,to=100,len=200)
myV<-myV^2
myV[myV<50]
## [1] 1.000000 2.242469 3.979925 6.212368 8.939800 12.162218 15.879624
## [8] 20.092018 24.799399 30.001768 35.699124 41.891467 48.578799
ADVANCED: Here is another way of doing this using the pipe operator that avoids intermediate expressions. If this doesn’t make sense, please ignore–its a bit advanced.
library(magrittr)
seq(from=1,to=100,len=200) %>% .^2 %>% .[.<50]
## [1] 1.000000 2.242469 3.979925 6.212368 8.939800 12.162218 15.879624
## [8] 20.092018 24.799399 30.001768 35.699124 41.891467 48.578799
myMat<-matrix(1:12,nrow=3,ncol=4)
myMat
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
myMat[2,3]*myMat[3,2]
## [1] 48
rownames(myMat)<-c('a','b','c')
colnames(myMat)<-c('1','2','3','4')
myMat
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
Alternative way
dimnames(myMat)<-list(c('a','b','c'),c('1','2','3','4'))
myMat
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
my3d<-array(c(rep(1:12,3)),dim=c(3,4,3))
dimnames(my3d)<-list(c('a','b','c'),c('1','2','3','4'),
c('1d','2d','3d'))
my3d
## , , 1d
##
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
##
## , , 2d
##
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
##
## , , 3d
##
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
my3d[2,,2]*my3d[3,3,3]
## 1 2 3 4
## 18 45 72 99
rep(c(1,2),5)
## [1] 1 2 1 2 1 2 1 2 1 2
myVect<-1:10
myVect^2
## [1] 1 4 9 16 25 36 49 64 81 100
myVect^3
## [1] 1 8 27 64 125 216 343 512 729 1000
myDataFrame<-data.frame(myVect,myVect^2,myVect^3)
head(myDataFrame)
ADVANCED: Here is another way of doing this using the pipe operator that avoids intermediate expressions. If this doesn’t make sense, please ignore–its a bit advanced.
library(magrittr)
myVect %>% data.frame(.,.^2, .^3) -> myDataFrame
colnames(myDataFrame)<-c('Original', 'Squared', 'Cubed')
plot(Original~Cubed,data=myDataFrame)
Advanced. Alternative method using Tidyverse dialect of R.
library(dplyr)
##
## 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
library(magrittr)
library(ggplot2)
myDataFrame %>%
select(Original, Cubed) %>%
ggplot(aes(x = Cubed, y = Original)) +
geom_point()
Below is some information on the use of R markdown used in this R notebook.
This is an R Markdown 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.
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).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.