[R] Conditional sampling?

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Feb 4 14:22:25 CET 2010

selection <- subset(somedf, col1 != "a")
sample(selection$col2, 2, replace = TRUE) 

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen

tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Olga Lyashevska
Verzonden: donderdag 4 februari 2010 14:05
Aan: r-help op r-project.org
Onderwerp: [R] Conditional sampling?


I have a dataframe consisting of two columns.

 > col1<-factor(c("a","a","b","b","c","c"))
 > col2<-factor(c("a","b","c","d","e","f"))
 > somedf<-data.frame(col1,col2)
 > somedf
     col1   col2
1      a      d
2      a      e
3      b      f
4      b      g
5      c      h
6      c      i

 > sample(col1,2,replace=T)
[1] b c
Levels: a b c

Now I want to sample from col2, but I want to restrict a vector I am
going to sample from to only those elements that correspond to col1 That
is, I want to take a sample from f, g, h, i.  Elements corresponding to
level a need to be dropped.

    col1    col2

3      b      f
4      b      g
5      c      h
6      c      i

Any ideas?
Thanks in advance!

R-help op r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

More information about the R-help mailing list