'Remove random row from dataframe if odd number of rows
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.
Solution 1:[1]
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() )
Solution 2:[2]
Just use if:
df <- if(nrow(df) %% 2 != 0) df[-sample(x = nrow(df), size = 1),]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akrun |
| Solution 2 | Maël |
