Skip to content

Instantly share code, notes, and snippets.

View lundquist-ecology-lab's full-sized avatar

Lundquist Ecology Lab lundquist-ecology-lab

View GitHub Profile
# Importing built in data
data(iris) # This imports the built-in Iris dataset
print(iris$Species) # Example code, printing the species of Iris in the dataset
# List all built-in datasets
data()
# Download data from the internet
# If the file is in .csv format
tmp <- tempfile(fileext = ".csv")
download.file(url = "https://data.cdc.gov/resource/9bhg-hcku.csv",
destfile = tmp)
covid_data <- read.csv(tmp, header = TRUE, stringsAsFactors = TRUE)
head(covid_data)
# How to import local data files
# For .csv files
data_name <- read.csv("/path/to/file_name.csv", head = TRUE,
stringsAsFactors = TRUE)
head(data_name)
# For .xlxs
library(readxl)
data_name <- read_excel("/path/to/file/_name.xlsx")
@lundquist-ecology-lab
lundquist-ecology-lab / summary_statsitics.r
Last active December 27, 2022 01:15
Summary statistics in R with examples
# Calculating mean, and other summary statistics in R with examples
# Set up
library(palmerpenguins)
data(penguins)
penguins <- na.omit(penguins)
# Mean
mean(penguins$bill_length_mm)
@lundquist-ecology-lab
lundquist-ecology-lab / binomial_test.r
Created December 27, 2022 01:20
Running a binomial test in R
# Six-sided die flipped 24 times and lands on three exactly 6 times
binom.test(x = 6, n = 24, p = 1 / 6, alternative = "greater")
# Flip coin 30 times and it lands on heads exactly 19 times
binom.test(x = 19, n = 30, p = 1 / 2, alternative = "greater")
@lundquist-ecology-lab
lundquist-ecology-lab / chi-square.r
Created December 27, 2022 01:23
Example chi-square goodness of fit test in R
# Example chi-square
chi_square <- ((50 - 56.25)^2 / 56.25) +
((20 - 18.75)^2 / 18.75) +
((20 - 18.75)^2 / 18.75) +
((10 - 6.25)^2 / 6.25)
print(chi_square)
## Chi-square distribution
pchisq(3.11, df = 3, lower.tail = FALSE)
@lundquist-ecology-lab
lundquist-ecology-lab / anova.r
Last active December 27, 2022 01:32
Running a one-way ANOVA in R with examples
data(iris)
df <- iris
# Box plots
boxplot(Petal.Width ~ Species, data = df)
# Strip plot
library(lattice)
stripplot(Petal.Width ~ Species, data = df, jitter = TRUE)
@lundquist-ecology-lab
lundquist-ecology-lab / wilcox_test.r
Created December 27, 2022 01:41
Running Wilcoxson signed rank test in R
data(iris)
df <- subset(iris, iris$Species == "setosa")
wilcox.test(df$Sepal.Width, df$Sepal.Length)
@lundquist-ecology-lab
lundquist-ecology-lab / kruskal_wallis.r
Created December 27, 2022 01:42
Running a Kruskal-Wallace test in R
data(iris)
df <- iris
kruskal.test(Petal.Width ~ Species, data = df)
@lundquist-ecology-lab
lundquist-ecology-lab / correlation_regression.r
Created December 27, 2022 01:48
Correlation and regression in R with examples
library(palmerpenguins)
data(penguins)
penguins <- na.omit(penguins)
# Correlation
cor.test(penguins$bill_length_mm, penguins$bill_depth_mm)
# Regression (linear model)
model_1 <- lm(bill_length_mm ~ bill_depth_mm, data = penguins)
summary(model_1)