Skip to content

Instantly share code, notes, and snippets.

@pvanheus
Created September 11, 2019 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvanheus/4345834da6867d30a6f340ab9b1bc794 to your computer and use it in GitHub Desktop.
Save pvanheus/4345834da6867d30a6f340ab9b1bc794 to your computer and use it in GitHub Desktop.
library(tidyverse)
surveys <- read_csv("data_raw/portal_data_joined.csv")
str(surveys)
select(surveys, plot_id, species_id, weight)
select(surveys, -record_id, -species_id)
filter(surveys, year == 1995)
surveys2 <- filter(surveys, weight < 5)
surveys_sml <- select(surveys2, species_id, sex, weight)
surveys_sml <- select(filter(surveys, weight < 5),
species_id, sex, weight)
# to get a pipe do Ctrl - Shift - M
# or on a Mac Cmd - Shift - M
# or type %>%
surveys %>%
filter(weight < 5) %>%
select(species_id, sex, weight)
surveys %>%
select(plot_id, sex)
select(surveys, plot_id, sex)
surveys %>%
filter(weight < 5) %>%
select(species_id, sex, weight)
surveys_sml <- surveys %>%
filter(weight < 5) %>%
select(species_id, sex, weight)
surveys %>%
filter(year < 1995) %>%
select(year, sex, weight)
surveys %>%
mutate(weight_kg = weight / 1000)
str(surveys)
new_tbl <- surveys %>%
mutate(weight_kg = weight / 1000)
View(new_tbl)
# this makes an error because == is a comparison
new_tbl <- surveys %>%
mutate(weight_kg == weight / 1000)
surveys %>%
mutate(weight_kg = weight / 1000,
weight_lb = weight_kg * 2.2) %>%
head()
surveys %>%
filter(!is.na(weight)) %>%
mutate(weight_kg = weight / 1000,
weight_lb = weight_kg * 2.2) %>%
head()
# form1
surveys %>%
filter(!year < 1995) %>%
head()
# form2 - equivalent to form1
surveys %>%
filter(year >= 1995) %>%
head()
surveys %>%
group_by(sex) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
surveys %>%
group_by(sex)
surveys %>%
summarize(mean_weight = mean(weight))
surveys %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
surveys %>%
group_by(sex) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
data_overview <- surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
View(data_overview)
surveys %>%
filter(!is.na(weight)) %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight)) %>%
print(n = 64)
surveys %>%
filter(!is.na(weight)) %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight),
min_weight = min(weight)) %>%
arrange(min_weight)
surveys %>%
filter(!is.na(weight)) %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight),
min_weight = min(weight)) %>%
arrange(min_weight, mean_weight)
surveys %>%
filter(!is.na(weight)) %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight),
min_weight = min(weight)) %>%
arrange(desc(min_weight))
# Counting data by criteria
surveys %>%
count(sex)
surveys %>%
count(year < 1995)
surveys %>%
count(sex, species_id)
surveys %>%
count(sex, species_id) %>%
arrange(species_id, desc(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment