2

这只是我想做的小例子(不完全是,但类似的问题):

test1 <- data.frame (matrix(sample( c(1,2,3,NA),15, replace = TRUE), 5,3))
 X1 X2 X3
1 NA NA  2
2  1  3  3
3 NA  1 NA
4 NA  2 NA
5  2 NA  3

我想用 1:chr 的 5 个不同的种子循环这个过程。然后我想按列组合生成的数据框:

X1  X2  X3 X1.1 X2.1 X3.1  ... so on (r will accept two variables with same name) 

 X1 X2 X3  X1 X2 X3 X1 X2 X3 X1 X2 X3 X1 X2 X3
1 NA NA  2 1 NA NA  2 1 NA NA  2 1 NA NA  2
2  1  3  3 2  1  3  3 2  1  3  3 2  1  3  3
3 NA  1 NA 3 NA  1 NA 3 NA  1 NA  3 NA  1 NA 
4 NA  2 NA 3 NA  1 NA 3 NA  1 NA 3 NA  1 NA 
5  2 NA  33 NA  1 NA 3 NA  1 NA 3 NA  1 NA 

这是我的试验:

nchr = 1:5
seed <- round(runif(nchr)*1000000)

funct <- function (x){
set.seed <- seed[x]
test1 <- data.frame (matrix(sample( c(1,2,3,NA),15, replace = TRUE), 5,3))
return (test1)
}

sapply(nchr, funct)

我怎样才能实现我的目标?

4

1 回答 1

5
> do.call(cbind, lapply(nchr, funct) )
  X1 X2 X3 X1 X2 X3 X1 X2 X3 X1 X2 X3 X1 X2 X3
1 NA  1  2  2  2  1  2  1  2  3  1 NA NA  1  3
2  1  1  3 NA NA NA  3  2 NA  3  1  1  3  2  1
3  3  3  2  2  2  3  2  2  3  1  1  1  2 NA  2
4  1  2  1  2  1  1 NA  2  3  1  2  1  2  2 NA
5  2  2  1  1  1 NA  1  3  2  3 NA  3  3  2  3
于 2012-07-04T19:55:53.350 回答