Home > Enterprise >  Remove random row from dataframe if odd number of rows
Remove random row from dataframe if odd number of rows

Time:04-26

I am hoping to randomly remove a row from my dataframe when there are an odd number of rows in the data. To do this, I've attempted this approach:

df <- tibble(value 1:100) # Creating dataframe

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.

Any help would be appreciated! Thank you.

CodePudding user response:

We may use slice with if/else condition instead of case_when as ifelse/case_when requires all arguments to be of same length. According to ?case_when

Both LHS and RHS may have the same length of either 1 or n. The value of n must be consistent across all cases. The case of n == 0 is treated as a variant of n != 1.

library(dplyr)
df %>% 
  slice(if(n() %% 2 != 0) -sample(row_number(), 1) else row_number() )

CodePudding user response:

Just use if:

df <- if(nrow(df) %% 2 != 0) df[-sample(x = nrow(df), size = 1),]
  • Related