[R] Randomly select one row by group from a matrix

Ulrik Stervbo ulrik.stervbo at gmail.com
Thu May 18 17:45:05 CEST 2017

Hi Marine,

your manipulation of the matrix is quite convoluted, and it helps to expand
a bit:

test_lst <- split(test, test[,c("id")])

after splitting, your matrix has gone back to be a plain vector, which
makes the sampling fail.

The reason is that, a matrix - behind the scenes - is a vector with a
dimension and when splitting the matrix you lose the dimension information.

Do you really need to work with a matrix? I prefer data.frames because I
can mix different types. Also with data.frame you can use the functionality
of the dplyr library, which also makes things more readable:


test_df <- data.frame(xcor = rnorm(8), ycor = rnorm(8), id = c(1, 2))

grouped_test_df <- group_by(test_df, id)
sample_n(grouped_test_df, 1)


On Thu, 18 May 2017 at 17:18 Marine Regis <marine.regis at hotmail.fr> wrote:

> Hello,
> I would like to randomly select one row by group from a matrix. Here is an
> example where there is one row by group. The code gives an error message:
> test <- matrix(c(4,4, 6,2, 1,2), nrow = 2, ncol = 3, dimnames = list(NULL,
> c("xcor", "ycor", "id")))
> do.call(rbind, lapply(split(test, test[,c("id")]), function(x)
> x[sample(nrow(x), 1), ]))
>  Show Traceback
>  Rerun with Debug
> Error in sample.int(length(x), size, replace, prob) :
>   invalid first argument
> How can I modify the code so that it works when there are several rows or
> one row for a given group?
> Thanks very much for your time
> Have a nice day
> Marine
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list