Created
February 2, 2022 14:18
-
-
Save padpadpadpad/f01a514a92bdd0f6d81e2273af1628fb to your computer and use it in GitHub Desktop.
Making a presence absence dataset from only presences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load necessary packages | |
library(dplyr) | |
library(tidyr) | |
# create sample dataset | |
am_i_here <- data.frame(time_step = rep(1:5, each = 10)) %>% | |
group_by(time_step) %>% | |
mutate(individual = sample(letters, 10)) %>% | |
ungroup() | |
# time steps and a bunch of "caught" individuals in that time step | |
am_i_here | |
# create master dataset | |
master <- am_i_here %>% | |
expand(time_step, individual) | |
# add a column to am_i_here so that presence is defined | |
am_i_here$presence <- 1 | |
new_master <- left_join(master, am_i_here) | |
# lots of NAs (absences) | |
filter(new_master, is.na(presence)) | |
# make these NA | |
new_master$presence[is.na(new_master$presence)] <- 0 | |
filter(new_master, is.na(presence)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment