I am hoping to randomly remove a row from my data frame when there are an odd number of rows in the data. To do this, I've attempted this approach:
library(dplyr)
df <- tibble(value 1:100) # Creating data frame
df <-
case_when(
nrow(df) %% 2 == 0 ~ df, # If even # of rows, keep df as is
nrow(df) %% 2 != 0 ~ df[-sample(x = nrow(df), size = 1),] # If odd number of rows, randomly sample one row and remove it from df
)
I receive
Error: Can't use NA as column index with
[at position 1.