Random subsampling in column (r) -


is possible random subsample (i.e size 50) entire column?

input example:

pa 0 pb 0 pc 127 pd 0 pe 13 pf 39 pg 0 ph 113 pi 0 

output example (size 50, random subsampled):

pa 0 pb 0 pc 22 pd 0 pe 2 pf 8 pg 0 ph 18 pi 0 

any ideas?

try

indx <- df1$v2!=0 df1$v2[indx] <- sample(50, sum(indx), replace=false)  

update

for getting subsamples based on condition values should less original value

f1 <- function(x, n){   indx <- x!=0   v1 <- sample(n, sum(indx), replace=true)   while(any(v1 > x[indx])){   v1 <- sample(n, sum(indx), replace=true)   }  x[indx] <- v1  x}   set.seed(24)  f1(df1$v2, 50)  #[1]  0  0 15  0 12 36  0 26  0 

or use repeat

  f2 <- function(x, n){    indx <- x!=0    repeat{     v1 <- sample(n, sum(indx), replace=true)     if(all(v1 <x[indx])) break     }    x[indx] <- v1    x}  set.seed(24)  f2(df1$v2, 50)  #[1]  0  0 15  0 12 36  0 26  0 

data

df1 <- structure(list(v1 = c("pa", "pb", "pc", "pd", "pe", "pf", "pg",  "ph", "pi"), v2 = c(0l, 0l, 127l, 0l, 13l, 39l, 0l, 113l, 0l)),  .names = c("v1",  "v2"), class = "data.frame", row.names = c(na, -9l)) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -