Search code examples
rrandomdatatablesample

Randomly selecting 50 columns in R data table results in table with only 50 rows. How can I fix this?


I'm trying to randomly select 50 columns in a data table in R (the original table has 110 columns and 1000+ rows). But when I run the code, the resulting table only has 50 rows (I am not trying to filter out the rows, only the columns).

randomTable = sample_n(ogTable, 50, axis = 'columns')

I looked up this issue and it seems like this function doesn't display all rows if it exceeds the number of resulting columns, but I could not find a way to get around this.


Solution

  • This one-liner works:

    randomTable <- ogTable[, c(1:2, sample(3:ncol(ogTable), 50)), with = FALSE]
    

    A reprex:

    library(data.table)
    set.seed(1)
    test <- data.table(iris)
    randomTable <- test[, c(1:2, sample(3:ncol(test), 2)), with = FALSE]
    
    head(randomTable)
    Sepal.Length Sepal.Width Petal.Length Petal.Width
    1          5.1         3.5          1.4         0.2
    2          4.9         3.0          1.4         0.2
    3          4.7         3.2          1.3         0.2
    4          4.6         3.1          1.5         0.2
    5          5.0         3.6          1.4         0.2
    6          5.4         3.9          1.7         0.4