On Wed, 1 Jun 2005 17:05:52 +0200 Sofyan Iyan wrote:> Dear R-helper, > How could I count only some variable was exist after running sample > (random) function.I'm not completely sure what you want, but maybe you want to look at table() and try print(table(x)) instead of print(x). Those levels with 0 occurences don't occur in the random sample. Z> For example, > > > testx <- factor(c("Game","Paper","Internet","Time","Money")) > > for(i in 1:2) { > + x <- sample(testx,replace=TRUE) > + print(x) > + } > [1] Money Money Time Internet Time > Levels: Game Internet Money Paper Time > [1] Time Money Game Money Money > Levels: Game Internet Money Paper Time > > > > The result above Game, Internet, Money, and Time only exist and > "Paper" was missing. > Best, Sofyan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Dear R-helper, How could I count only some variable was exist after running sample (random) function. For example,> testx <- factor(c("Game","Paper","Internet","Time","Money")) > for(i in 1:2) {+ x <- sample(testx,replace=TRUE) + print(x) + } [1] Money Money Time Internet Time Levels: Game Internet Money Paper Time [1] Time Money Game Money Money Levels: Game Internet Money Paper Time>The result above Game, Internet, Money, and Time only exist and "Paper" was missing. Best, Sofyan
> Dear R-helper, > How could I count only some variable was exist after running sample > (random) function.?> For example, > > > testx <- factor(c("Game","Paper","Internet","Time","Money")) > > for(i in 1:2) { > + x <- sample(testx,replace=TRUE) > + print(x) > + } > [1] Money Money Time Internet Time > Levels: Game Internet Money Paper Time > [1] Time Money Game Money Money > Levels: Game Internet Money Paper Time > > > > The result above Game, Internet, Money, and Time only exist > and "Paper" was missing. Best, Sofyan >sample(testx, replace=FALSE) Is that what you want?? Best, Matthias
> Dear R-helper, > How could I count only some variable was exist after running sample > (random) function.Or is this what you want? Tab <- table(sample(testx, replace=TRUE)) Tab[ Tab > 0 ]> For example, > > > testx <- factor(c("Game","Paper","Internet","Time","Money")) > > for(i in 1:2) { > + x <- sample(testx,replace=TRUE) > + print(x) > + } > [1] Money Money Time Internet Time > Levels: Game Internet Money Paper Time > [1] Time Money Game Money Money > Levels: Game Internet Money Paper Time > > > > The result above Game, Internet, Money, and Time only exist > and "Paper" was missing. Best, Sofyan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html >
Or perhaps> f <- factor(letters[1:2], levels=letters[1:3]) > f[1] a b Levels: a b c> levels(f[, drop=TRUE])[1] "a" "b" Andy> From: TEMPL Matthias > > > Dear R-helper, > > How could I count only some variable was exist after running sample > > (random) function. > > Or is this what you want? > > Tab <- table(sample(testx, replace=TRUE)) > Tab[ Tab > 0 ] > > > For example, > > > > > testx <- factor(c("Game","Paper","Internet","Time","Money")) > > > for(i in 1:2) { > > + x <- sample(testx,replace=TRUE) > > + print(x) > > + } > > [1] Money Money Time Internet Time > > Levels: Game Internet Money Paper Time > > [1] Time Money Game Money Money > > Levels: Game Internet Money Paper Time > > > > > > > The result above Game, Internet, Money, and Time only exist > > and "Paper" was missing. Best, Sofyan > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read > > the posting guide! http://www.R-project.org/posting-guide.html > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Thanks for your help. How about if I count all. This below I have fixed my code which used table. I need the result: Game Internet Money Paper Time 4 1 1 4 0 Regards, Sofyan> testx <- factor(c("Game","Paper","Internet","Time","Money")) > for(i in 1:2) {+ x <- sample(testx,replace=TRUE) + print(x) + print(table(x)) + } [1] Game Paper Game Paper Paper Levels: Game Internet Money Paper Time x Game Internet Money Paper Time 2 0 0 3 0 [1] Game Game Internet Paper Money Levels: Game Internet Money Paper Time x Game Internet Money Paper Time 2 1 1 1 0>On 6/1/05, TEMPL Matthias <Matthias.Templ at statistik.gv.at> wrote:> > Dear R-helper, > > How could I count only some variable was exist after running sample > > (random) function. > > Or is this what you want? > > Tab <- table(sample(testx, replace=TRUE)) > Tab[ Tab > 0 ] >
Dear R-help again, I have a result something like this, [[1]] [1] "Game" "Internet" [[2]] [1] "Game" "Internet" [[3]] [1] "Game" "Time" How could I make the result above like, [1] "Game","Internet","Time" Regards, Sofyan On 6/1/05, Sofyan Iyan <sofyan.iyan at gmail.com> wrote:> Dear R-helper, > How could I count only some variable was exist after running sample > (random) function. > For example, > > > testx <- factor(c("Game","Paper","Internet","Time","Money")) > > for(i in 1:2) { > + x <- sample(testx,replace=TRUE) > + print(x) > + } > [1] Money Money Time Internet Time > Levels: Game Internet Money Paper Time > [1] Time Money Game Money Money > Levels: Game Internet Money Paper Time > > > > The result above Game, Internet, Money, and Time only exist and > "Paper" was missing. > Best, Sofyan >
Try unique(unlist(myList)). Andy> From: Sofyan Iyan > > Dear R-help again, > I have a result something like this, > [[1]] > [1] "Game" "Internet" > > [[2]] > [1] "Game" "Internet" > > [[3]] > [1] "Game" "Time" > > How could I make the result above like, > > [1] "Game","Internet","Time" > > Regards, Sofyan > > On 6/1/05, Sofyan Iyan <sofyan.iyan at gmail.com> wrote: > > Dear R-helper, > > How could I count only some variable was exist after running sample > > (random) function. > > For example, > > > > > testx <- factor(c("Game","Paper","Internet","Time","Money")) > > > for(i in 1:2) { > > + x <- sample(testx,replace=TRUE) > > + print(x) > > + } > > [1] Money Money Time Internet Time > > Levels: Game Internet Money Paper Time > > [1] Time Money Game Money Money > > Levels: Game Internet Money Paper Time > > > > > > > The result above Game, Internet, Money, and Time only exist and > > "Paper" was missing. > > Best, Sofyan > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >