[R] Removing values from a vector

Liaw, Andy andy_liaw at merck.com
Fri Apr 16 03:55:24 CEST 2004


Use negative indexing; e.g., pop <- pop[-c(3, 7)] removes the 3rd and 7th
element of pop.

Andy

> From: Joseph Sakshaug
> 
> Dear R-help faithful,
> 
> I am trying to build a program which will take repeated samples (w/o
> replacement) from a population of values. The interesting 
> catch is that I
> would like the sample values to be removed from the 
> population, after each
> sample is taken.
> 
> For example:
> 
> pop<-1:10
> 
> sample(pop, 2) = lets say, (3, 7)
> 
> ## This is where I would like values (3, 7) to be removed from the
> population vector, giving a new "current" population vector:
> 
> "new" pop = [1, 2, 4, 5, 6, 8, 9, 10]
> and has length 8 instead of 10.
> 
> 
> At first I tried to run a loop and comparison of (sample.pop 
> == pop) and
> IF a FALSE was found the loop would store the [ith] value of 
> sample.pop
> in a new vector called pop.current. Then, due to my beginning 
> programming
> skills, I kind of got stuck linking pop.current to the 
> population which is
> to be sampled from...
> 
> 
> I know that in PHP there is a function called unset() which 
> removes values
> from an array. Does R have an equivalent function?
> 
> Thanks in advance.
> 
> Joe
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list