Â
Â
life <- 42
# a number, 42, has been assigned to a variable named life.
Â
numbers <- 1:1000
# a series of numbers, from 1 to 1000, have been assigned to a variable named numbers.
Â
roots <- sqrt(numbers)
# each individual number within the variable "numbers" has been square rooted and put under the variable "roots"
Â
par(mar = c(4, 4, 1, 1), xaxs = "i", yaxs = "i")
plot(numbers,roots,
xlab = "numbers",
ylab = "roots",
xlim = c(0, 1000), ylim = c(0, 35),
bty = "l", las = 1, cex.axis = 0.9,
pch = 20, col = "#CD0CFC", cex = 0.5
)
# roots has been plotted against numbers, with roots on the y axis and numbers on the x axis using the function plot().
# used par(mar = c(4, 4, 1, 1) to adjust margins.
# xlab and ylab have been used to assign titles to each axis.
# xlim and ylim have been used to adjust the spacing between points in each axis to make sure all data is visible on the curve.
# bty = "l" has been used to only maintain 2 lines for each axis rather than having 4 lines forming a box around the data.
# las = 1 has been added to make y-axis points horizontal.
# xaxs = "i" and yaxs = "i" have been added to ensure graph has a 0,0 origin.
# cex.axis = 0.9 was used to make the text 10% smaller.
# pch changes the color of the plotting symbol, while col adjusts the color of the plotting symbol and cex adjusts its size.
Â
?abs()
## starting httpd help server ... done
# Used '?' function to see what abs() does.
Â
par(mar = c(4, 4, 4, 2), xaxs = "i", yaxs = "i")
hist(rnorm(5000, mean=57, sd=12),
breaks=20,
main ="Task 6",
xlab ='numbers within distribution',
xlim = c(0,120),
col = "#0C51FC",
bty = "l", las = 1, cex.axis = 0.9,)
abline(v = 57, col = "#000000")
# plotted histogram using hist() function and included rnorm() function set to produce 5000 random numbers with a mean of 57 and a standard deviation (sd) of 12.
# used par(mar = c(4, 4, 4, 2) to adjust margins.
# used xaxs = "i" and yaxs = "i" to set histogram origin to 0,0.
# used breaks to adjust number of bars visible on histogram.
# used main ="Task 6" to make the title 'Task 6'.
# added xlab to add title 'numbers within distribution' to x-axis.
# used col ="#0C51FC" to make the histogram blue.
# added bty ="1" to only have axis lines.
# xlim has been used to adjust the spacing between points in the y axis to make sure all bars visible on the x-axis.
# bty = "l" has been used to only maintain 2 lines for each axis.
# cex.axis = 0.9 was used to make the text 10% smaller.
# las = 1 has been added to make y-axis points horizontal.
# used abline to add vertical to display the mean.
Â
numbers[1:83]
## [1] 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] 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] 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] 76 77 78 79 80 81 82 83
# indexing has been used to extract first 83 numbers within the variable numbers.
#'[]'signifys an index and 1:83 means retrieve values 1:83.
Â
mean(numbers[1:83])
## [1] 42
# Used mean() function to calculate mean of first 83 numbers within the variable numbers.
Â
myMatrix <- matrix(data = numbers, ncol = 10, byrow = FALSE,
dimnames = NULL)
# a matrix under a variable named 'myMatrix' has been made using the variable 'numbers'. It contains 10 columns.
sum(myMatrix[,10])
## [1] 95050
# the sum of the variable 'myMatrix' has been calculated using indexing and the function sum().
Â
coursework <- list(numbers, roots, myMatrix)
# a list named coursework has been created containing the variables myMatrix, numbers and roots using the list() function.
Â
class(numbers)
## [1] "integer"
# the function class() has been used to identify the class of variable 'numbers'.
Â
class(roots)
## [1] "numeric"
# the function class() has been used to identify the class of variable 'roots'.
Â
class(myMatrix)
## [1] "matrix" "array"
# the function class() has been used to identify the class of variable 'myMatrix'.
Â
sum(coursework[[2]])
## [1] 21097.46
# the list called 'coursework'has been indexed to isolate values for the variable roots using double brackets and the position of 'roots' within the list.
#used the sum() function to find the sum of roots.
Â
Â
setwd("C:/Users/mpkyo.LAPTOP-IBPP1IPC/OneDrive - University of Essex/Year 2 Biochemsitry/Autumn Term/BS231 R Studio 2/Assignements/Worksheet 1")
#set working directory to my current working directory.
Â
list.files("C:/Users/mpkyo.LAPTOP-IBPP1IPC/OneDrive - University of Essex/Year 2 Biochemsitry/Autumn Term/BS231 R Studio 2/Assignements/Worksheet 1")
## [1] "mockfile.rmd" "owls.csv"
## [3] "rscript_programs.R" "rscriptjokes.R"
## [5] "Things to do for worksheet 1.docx" "Worksheet1.html"
## [7] "Worksheet1.pdf" "Worksheet1.rmd"
## [9] "Worksheet1_files" "Worksheet1_PK.html"
#used function list.files() to make list of files in my working directroy.
Â
owls <- read.csv("owls.csv")
head(owls, 10)
## X common.name latin size.cm wingspan.cm weight.g
## 1 1 Barn Owl Tyto alba 41.76713 104.0338 464.8648
## 2 2 Barn Owl Tyto alba 41.95082 110.0214 457.1181
## 3 3 Barn Owl Tyto alba 38.52050 108.3817 463.8821
## 4 4 Barn Owl Tyto alba 41.62556 104.0448 460.2816
## 5 5 Barn Owl Tyto alba 39.02970 108.8678 464.7712
## 6 6 Barn Owl Tyto alba 41.58190 104.1891 462.2993
## 7 7 Barn Owl Tyto alba 42.14086 107.6116 459.6003
## 8 8 Barn Owl Tyto alba 40.93317 108.6951 459.8983
## 9 9 Barn Owl Tyto alba 43.58364 106.9494 462.5507
## 10 10 Barn Owl Tyto alba 39.24096 107.8284 459.0347
# imported data from owls.csv using the read.csv function and assigned it to a variable called 'owls'.
# used function head() to inspect first 10 rows of dataset owls.
Â
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
##
## 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
owls %>% select(common.name, wingspan.cm) %>% filter(common.name == "Barn Owl") %>% summarise(mean = mean(wingspan.cm), standard.deviation = sd(wingspan.cm))
## mean standard.deviation
## 1 107.4148 2.032915
# Used pipeline with the following functions:
# select() to only get values from certain columns
# filter() to only retrieve owls with a common name of Barn OWl.
# summarise() to get the standard deviation using sd() and mean using mean() of the wingspan of Barn Owls.
Â
owl_table <- owls %>%
group_by(common.name) %>%
summarise(
size_cm = mean(size.cm),
wingspan_cm = mean(wingspan.cm),
weight_g = mean(weight.g))
print(owl_table, digits = 5)
## # A tibble: 8 × 4
## common.name size_cm wingspan_cm weight_g
## <chr> <dbl> <dbl> <dbl>
## 1 Barn Owl 40.9 107. 461.
## 2 Great Grey Owl 70.1 151. 1004.
## 3 Great Horned Owl 52.1 110. 1381.
## 4 Long-eared Owl 36.5 99.6 251.
## 5 Northern Hawk Owl 40.1 70.5 326.
## 6 Northern Saw-whet Owl 18.3 39.5 80.4
## 7 Short-eared Owl 36.6 100. 346.
## 8 Snowy Owl 59.8 124. 1303.
# assigned table as variable named owl_table.
# used pipeline to create table with mean size, wingspan and weight for each owl specie by using:
# group_by(common.name) to group owl species by name.
# mean() to find mean size, wingspan and weight.
# used print() function to print owl_table.
weight_kg = owls$weight.g/1000
owl_table2 <- cbind(owl_table, weight_kg)
head(owl_table2, 6)
## common.name size_cm wingspan_cm weight_g weight_kg
## 1 Barn Owl 40.86715 107.41475 460.97393 0.4648648
## 2 Great Grey Owl 70.14600 150.74414 1004.13233 0.4571181
## 3 Great Horned Owl 52.08950 109.63377 1381.02988 0.4638821
## 4 Long-eared Owl 36.46878 99.64831 251.08521 0.4602816
## 5 Northern Hawk Owl 40.11852 70.45838 325.82226 0.4647712
## 6 Northern Saw-whet Owl 18.30085 39.53849 80.43998 0.4622993
# created variable with weight in kg by dividing weight.g by 1000.
# assigned weight of owl species in kg to variable called weight_kg.
# assigned second table containing new column as variable named owl_table2.
# used print() function to print table.
Â
Â
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
distribution.wingspan <- ggplot(owls, aes(x= wingspan.cm, fill = common.name)) +
geom_density(alpha=0.6, color="black") + theme(legend.position="bottom") +
labs(title = "Wingspan Distribution Amongst Different Species of Owl",
x = "Wingspan/cm",
y = "Frequency"
)
distribution.wingspan
# loaded ggplot2 library for making histogram to show distribution.
# used ggplot to display distribution of wingspan of different species of owls;
# used common name as fill to separate distributions by species name.
# added title of both axis and graph with labs() function.
# used geom_density() function to adjust outline color of distributions and to adjust opacity as well.
# used them(legend.position="bottom") to move common.name legend to the bottom to reduce crowding of graph space.
Â
wingspan.size <- ggplot(data = owls, mapping = aes(x = size.cm, y = wingspan.cm, color = common.name)) + theme_minimal() + theme(legend.position="bottom") +
labs(title = "Wingspan of Different Species of Owl Plotted Against their Sizes",
x = "Size/cm",
y = "Wingspan/cm"
) +
geom_point( size = 1.5, pch = 19)
wingspan.size
# used ggplot to plot wingspan of different species of owls against their respective sizes using;
# common name as color to separate points by species name.
# added title of both axis and graph with labs() function.
# used geom_point() function to adjust size and style of points.
# used them(legend.position="bottom") to move common.name legend to the bottom to reduce crowding of graph space.
# used minimal theme to make graph easier to view.
Â
distribution.wingspan2 <- ggplot(owls, aes(x= wingspan.cm, fill = common.name)) +
geom_density(alpha=0.6, color="black") + theme(legend.position="bottom") +
labs(title = "Wingspan Distribution Amongst Different Species of Owl",
x = "Wingspan/cm",
y = "Frequency"
) + facet_grid(~common.name) + theme(text = element_text(size = 8))
distribution.wingspan2
# added facet_grid() function specifying species name to create facets for each species.
# used theme() to adjust text size to make graph better legible.
wingspan2 <- filter(owls, wingspan.cm > 75)
wingspan.size2 <- ggplot(data = wingspan2, mapping = aes(x = size.cm, y = wingspan.cm, color = common.name)) + theme_minimal() +theme(legend.position="bottom") +
labs(title = "Wingspan of Different Species of Owl Plotted Against their Sizes",
x = "Size/cm",
y = "Wingspan/cm"
) +
geom_point( size = 1.5, pch = 19)
wingspan.size2
# created variable wingspan2 to store filtered data meant to only return values of wingspan greater than 75 cm.
# used wingspan2 to plot wingspan greater than 75 cm against size.
# used minimal theme to make graph easier to view.