Reputation: 16469
Is there a way to random pick a column in a dataframe and then avoid randomly pick it again? This should pick a random column
random_data_vector = data[, sample(ncol(data), 1)]
but I'm not sure how to avoid picking the column again. I thought about removing the column completely but there might be a better approach
Upvotes: 0
Views: 24
Reputation: 7724
You can first sample the columns with
random_cols <- sample(ncol(data))
and then select the random vectors like this
random_data_vector1 <- my_df[, random_cols[1]]
random_data_vector2 <- my_df[, random_cols[2]]
The default setting of sample
is replace = FALSE
, thus in the random_cols
vector you won't have duplicated numbers and you won't select one column twice.
Upvotes: 2