How to sample random rows of a data frame in the R programming language. More details: statisticsglobe.com/r-sample-...
R code of the video:
data <- data.frame(x1 = 1:5, # Create example data
x2 = LETTERS[1:5],
x3 = c(4, 1, 9, 0, 1))
data # Print example data
##### Example 1
set.seed(12345) # Set seed for reproducibility
data_s1 <- data[sample(1:nrow(data), 3), ] # Sample rows of data with Base R
data_s1 # Print sampled data
install.packages("dplyr") # Install dplyr package
library("dplyr") # Load dplyr package
##### Example 2
set.seed(12345) # Set seed for reproducibility
data_s2 <- sample_n(data, 3) # Sample rows of data with dplyr
data_s2 # Print sampled data
Негізгі бет Sample Random Rows of Data Frame in R (2 Examples) | sample Function vs. sample_n of dplyr Package
Пікірлер: 16