s-news
[Top] [All Lists]

[S] sampling from a vector subset

To: <s-news@wubios.wustl.edu>
Subject: [S] sampling from a vector subset
From: "Valerie Zartarian" <VZARTARIAN@usgs.gov>
Date: Wed, 21 Apr 1999 10:32:18 -0400
Sender: owner-s-news@wubios.wustl.edu
My sincere thanks to each of you that took the time to answer my question from 
yesterday.  I received a number of different solutions and have attached them 
below  FYI.  I am always impressed with the generosity, efficiency, and 
abilities of the people on this news group.

Best regards,
Valerie


> I have a vector of the following form:
> 
> temp_c("R","N","N","R","M","M","R","R","M","R","R","N","R","R","N")
> 
> I want to replace a random sample of 3 of the "R"s with a "Z".  I would 
> greatly
> appreciate any insight into how to do this.  

Pat Burns:
rloc <- seq(along=temp)[match(temp, 'R', nomatch=0) > 0]
randloc <- sample(rloc, size=3, replace=F)
temp[randloc] <- 'Z'

Timothy Wade:
temp0_ifelse(temp=="R", "Z", temp)

Rachel Fewster:
foo <- function(vec)
{
        r.inds <- (1:length(vec))[vec == "R"]
        vec[sample(r.inds, 3, replace = F)] <- "Z"
        vec
}

Joel Dubin:
> temp[sample(seq(length(temp))[temp=='R'], size=3)] _ 'Z'
> temp
 [1] "Z" "N" "N" "R" "M" "M" "Z" "Z" "M" "R" "R" "N" "R" "R" "N"

Don McKenzie:
allnums <- c(1:length(temp))
temp.list <-
list(Rcells=allnums[temp=="R"],nums=runif(length(temp[temp=="R"])))
nums.to.change <-
  temp.list$Rcells[match(temp.list$nums,sort(temp.list$nums)[1:3]) !=
"NA"]
for (i in 1:3)
  temp[nums.to.change[i]] <- "Z"

Albyn Jones:
> match(temp,"R")
 [1]  1 NA NA  1 NA NA  1  1 NA  1  1 NA  1  1 NA
> n <- !is.na(match(temp,"R"))
> length(temp)
[1] 15
> (1:15)[n]
[1]  1  4  7  8 10 11 13 14
> sample((1:15)[n],3)
[1]  4 14  7

Peter Alspach:
your.vector <-      
temp_c("R","N","N","R","M","M","R","R","M","R","R","N","R","R","N")

your.vector[sample((1:length(your.vector))[your.vector=='R'],3)] <- 'Z'

Remy vande Ven:
t1 <- (1:length(temp))[as.logical(match(temp,"R"))]
temp[sample(t1[!is.na(t1)],3)] <- "Z"

Phil Spector:
temp[sample(seq(along=temp)[temp=="R"],3)] <- "Z"

Kevin Brand:
temp[sample(1:length(temp),3,F)]_"Z"

Rahul M. Dodhia:
del_order(temp!="R")[1:length(temp[temp=="R"])] #this gives you the
positions of all the R elements in temp

temp[sample(del,3)]_"Z"  #this takes a random sample of 3 elements from
del, and replaces the elements in those positions in temp with Z.

Samuel Buttrey:
rs <- temp[temp == "R"] # logical that's TRUE where temp equals R
which <- sample (sum(rs), 3)    # Choose three to be replaced
temp[rs][which] <- "Z"  # do the replace

Kenneth Butler:
Ø temp[sample(length(temp),prob=(temp=="R"),3)]<-"Z"

Bill Venables:
Rs <- seq(along = temp)[!is.na(match(temp, "R"))]
> Rs
[1]  1  4  7  8 10 11 13 14
> sam <- sample(Rs, 3)
> sam
[1]  8  1 14
> temp[sam] <- "Z"
> temp
 [1] "Z" "N" "N" "R" "M" "M" "R" "Z" "M" "R" "R" "N" "R" "Z" "N"

Erin Hodgess:
> zart(temp,"R","Z")
 [1] "R" "N" "N" "R" "M" "M" "R" "Z" "M" "R" "R" "N" "Z" "Z" "N"
> zart
function(x, y, ny)
{
#x is the input vector
#y is the value you wish to select
#ny is the new value
        z <- x
        xsel <- x == y
        xs <- sum(xsel)
        xl <- length(xsel)
        yy <- order(xsel)
        zz <- sample(yy[xs:xl], 3)
        z[zz] <- ny
        z
}

Alan Zaslavsky:
> X_temp
> XR_X=="R"
Ø X[XR][sample(sum(XR),3)]_"Z"

Anne York:
length(temp)
[1] 15

 (1:15)[temp=="R"] #indices of temp that equal "R"
[1]  1  4  7  8 10 11 13 14

 sample((1:15)[temp=="R"],size=3) # a sample of size 3 of the
                                  #  of temp = R


 temp[sample((1:15)[temp=="R"],size=3)]_"Z" #change those indices
                                            # to "Z"

Bill Dunlap:
Define the function which:
        which <- function(x) seq(along = x)[!is.na(x) & x]
so that, e.g.,
        > which(temp=="R")
        [1]  1  4  7  8 10 11 13 14
then do
        temp[sample(which(temp=="R"),size=3)] <- "Z"

John Wallace:
temp.r_seq(along=temp)[temp=="R"]
temp.z_temp
temp.z[sample(temp.r, 3)]_"Z"
temp.z

George Tomlinson:
temp[sample(seq(along=temp)[temp=="R"],size=3)]<-"Z"

Or, step by step:
ii <- seq(along=temp)   # make a vector of the indices of temp
xx <- ii[temp=="R"]             # find the indices of temp where temp=="Z" and 
puts
them in xx
zz<-sample(xx,size=3)   # takes a random sample of size 3 from the indices
and puts it in zz
temp[zz] <- "Z"         # puts "Z" into a random sample from the locations where
temp =="Z"

Edit_2_Kurali@sbphrd.com:
ind <- 1:length(temp)
samp <- sample(ind[temp=="R"], 3, replace=F) #sample from the index vector
   w/o replacement
temp[samp] <- "Z"; temp

Eusebio Arenal:
> temp[sample((1:length(temp))[temp == "R"], size = 3)] <- "Z"










-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>