2020-04-04

How to select a random sample in R

Title

Problem

We want to extract a random sample from a data frame in R.

Solution

  • Base package
set.seed(1)
starwars[sample(nrow(starwars), 10), ] # 10 filas
# Showing the first 5 columns
set.seed(1)
starwars[sample(nrow(starwars), 10), 1:5]
# A tibble: 10 x 5
   name            height  mass hair_color skin_color
                            
 1 Dexter Jettster    198 102   none       brown     
 2 Sebulba            112  40   none       grey, red 
 3 Luke Skywalker     172  77   blond      fair      
 4 Jar Jar Binks      196  66   none       orange    
 5 Bib Fortuna        180  NA   none       pale      
 6 Han Solo           180  80   brown      fair      
 7 Cliegg Lars        183  NA   brown      fair      
 8 Eeth Koth          171  NA   black      brown     
 9 Boba Fett          183  78.2 black      fair      
10 Yarael Poof        264  NA   none       white
  • dplyr
library(tidyverse)
set.seed(1)
starwars %>%
  sample_n(10) %>%
  select(1:5)
  • data.table
library(data.table)
set.seed(1)
data.table(starwars)[sample(.N, 10), 1:5]

No hay comentarios:

Publicar un comentario

Nube de datos