列(r)中的随机子采样

时间:2015-06-01 09:12:48

标签: r

可以随机子样本(即大小50)整列吗?

输入示例:

Pa 0
Pb 0
Pc 127
Pd 0
Pe 13
Pf 39
Pg 0
Ph 113
Pi 0

输出示例(大小50,随机子采样):

Pa 0
Pb 0
Pc 22
Pd 0
Pe 2
Pf 8
Pg 0
Ph 18
Pi 0

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试

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

更新

根据值应小于原始值

的条件获取子样本
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

或使用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))