I want to generate a set of 1000 random numbers between 1 and 20000 to then use those to subset a dataframe.
sample.int(20000, 2000, replace = TRUE)
Then I want to copy/paste these numbers into this vector
df_sub <- df[,c(65,25,1,6000,1056, ...)]
Is this possible?
CodePudding user response:
No need to copy/paste, just sample directly in the indexing operation:
For example, to randomly select 1000 rows between 1 and 20000 (with replacement):
df[sample(1:20000, 1000, replace=T),]
CodePudding user response:
You can use clipr to write the vector to your clipboard.
library(dplyr)
s <- sample.int(20000, 2000, replace = TRUE)
vectorToCopy <- s %>%
stringr::str_c(",", collapse=" ") %>% # sep each element by comma
stringr::str_sub(., 1, nchar(.)-1) %>% # remove last comma
str_c("c(", ., ")") # add parentheses
clipr::write_clip(vectorToCopy) # write to clipboard