-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabWeekTwo.Rmd
More file actions
128 lines (99 loc) · 2.58 KB
/
LabWeekTwo.Rmd
File metadata and controls
128 lines (99 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
title: "LabWeekTwo"
output: html_document
date: "2026-01-29"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
## Basic syntax
```{r eval=FALSE}
2+2
-3*5
(20-10)/2.5
7^2
abs(-10)
sqrt(81)
log(4)
log(8, base = 2)
round(123.456789, digits = 2)
x <- 7
y <- 4+4
my_name <- "Kelly Ho"
sqrt(x)
(x*y)/2
```
#Vectors and dataframes
```{r eval=FALSE}
primes <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
terms <- c("Michaelmas", "Hilary", "Trinity")
one_to_ten <- 1:10
decimals <- seq (0,10, by = 0.1)
rep(terms, times = 10)
length(primes)
sum(primes)
mean(primes)
median(primes)
max(primes)
min(primes)
one_to_ten > 6
students <- c("Kelly", "Eleanor", "Yubo")
grade <- c(100, 20, 20)
pass <- grade >= 50
my_dataframe <- data.frame(students, grade, pass)
View(my_dataframe)
```
#Loading a dataset
```{r eval=FALSE}
brexit <- read.csv("brexit.csv")
View(brexit)
dim(brexit)
colnames(brexit)
summary(brexit)
head(brexit)
```
#describing variables
```{r eval=FALSE}
brexit$percent_leave
mean(brexit$percent_leave)
median(brexit$percent_leave)
max(brexit$percent_leave)
summary(brexit$percent_leave)
mean(brexit$percent_UK_born) # produces NA (a missing value)
mean(brexit$percent_UK_born, na.rm = TRUE)
unique(brexit$region)
table(brexit$region)
```
```{r eval=FALSE}
brexit[1,2]
brexit[1:10, c(2,4)]
brexit$percent_leave[1]
brexit$percent_leave[1:10]
brexit$percent_leave[brexit$area == "Oxford"]
brexit$percent_leave[brexit$region == "Wales"]
brexit$percent_leave[brexit$median_age > 40]
mean(brexit$percent_leave[brexit$median_age > 40])
mean(brexit$percent_UK_born[brexit$median_age > 40], na.rm = T)
```
```{r eval=FALSE}
brexit$turnout <- NA
brexit$turnout <- brexit$total_votes/brexit$electorate
summary(brexit$turnout)
brexit$winner <- NA
brexit$winner[brexit$percent_leave > 50] <- "Leave"
brexit$winner[brexit$percent_leave < 50] <- "Remain"
table(brexit$winner)
write.csv(brexit, "brexit_new.csv", row.names=FALSE)
```