Reputation: 79
is possible to random subsample (i.e size 50) an 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?
Upvotes: 0
Views: 506
Reputation: 887881
Try
indx <- df1$v2!=0
df1$v2[indx] <- sample(50, sum(indx), replace=FALSE)
For getting the subsamples based on the condition that the values should be less than the 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
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))
Upvotes: 1