Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited body
Source Link
gented
  • 1.7k
  • 1
  • 17
  • 23

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10) mtcars <- data.table(mtcars) mtcars[sample(.N, 56)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 56: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10) mtcars <- data.table(mtcars) mtcars[sample(.N, 5)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 5: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10) mtcars <- data.table(mtcars) mtcars[sample(.N, 6)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 6: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 
added 36 characters in body
Source Link
gented
  • 1.7k
  • 1
  • 17
  • 23

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10)   mtcars <- data.table(mtcars) mtcars[sample(.N, 5)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 5: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10) mtcars[sample(.N, 5)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 5: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10)   mtcars <- data.table(mtcars) mtcars[sample(.N, 5)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 5: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 
Source Link
gented
  • 1.7k
  • 1
  • 17
  • 23

The data.table package provides the function DT[sample(.N, M)], sampling M random rows from the data table DT.

library(data.table) set.seed(10) mtcars[sample(.N, 5)] mpg cyl disp hp drat wt qsec vs am gear carb 1: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 2: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 4: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 5: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 5: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2