Pregunta

I have data in a form much like the output from aggregate, except that I do not have the original non-aggregated data.

Example:

 data <- data.frame(grade=letters[1:4], count=c(3,9,4,1))
  grade count
1     a     3
2     b     9
3     c     4
4     d     1

I would like to sample from this population of grades, e.g. using sample. What is the easiest way of taking a sample (without replacement) from summarized counts like this?

¿Fue útil?

Solución

Do you expect something like this?

> sample(with(data, rep(as.character(grade), count)), 10)
 [1] "b" "b" "d" "a" "c" "c" "b" "b" "c" "b"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top