R: Replicating a loop or function multiple times and adding results to data frame for each time -


i've written r loop , turned function takes in dataframe, original code , data frame below. goal repeat out function or loop 1000 times , end data frame has 1000 columns representing rowsums each row.name.

my goal, data frame looks this

row.names   rsum_s1  rsum_s2  rsum_s3  rsum_s4.....rsum_s1000  kc231       40       57       15       34 kc25498     34       39       567      23 kc087398    28       3747     25       1938 

x original data frame , looks this:

row.names   val2        val4        val3        val4 kc231       1.62e-08    3.29e-37    1.36e-14    0.29692426 kc25498     4.93e-01    4.93e-01    4.93e-01    0.49330053 kc087398    3.50e-01    1.18e-22    1.71e-08    0.35011743 

loop first wrote works give me rsum_s list.

  for(k in 1:length(colnames(x))) {           as.numeric(x[,k])         sample(x[,k])         x[,k]<-rank(x[,k],ties.method="min")         rsum_s<-rowsums(x) 

output of loop rank sums each row.name id in each row: rsum_s

structure(c(47, 142, 82), .names = c("kc231", "kc25498", "kc087398")) 

loop converted function

sim<-function(x) { #takes data.frame   for(k in 1:length(colnames(x))) {  #each column set numeric     as.numeric(x[,k])     sample(x[,k])  #randomly shuffle values in each column     x[,k]<-rank(x[,k],ties.method="min") #rank each randomly shuffled columns     rsum_s<-rowsums(x) #take sum of rows     return(rsum_s)     } } 

result of function in integers instead of whole numbers.

sim(dataframe1) kc231   kc25498 kc087398 18.24   37.47   32.350117  

i'm not sure doing wrong here. need loop 1000 times , append column of rank sums each time loop run data frame or replicate function sim 1000 times , convert results data frame work. if can me in completing task great

any appreciated.

i think meant write:

sim <- function(x) { #takes data.frame   for(k in 1:ncol(x)) {  #each column set numeric     x[,k] <- as.numeric(x[, k])     x[,k] <- sample(x[, k])  #randomly shuffle values in each column     x[,k] <- rank(x[, k], ties.method = "min") #rank each randomly shuffled columns   }   rsum_s <- rowsums(x) #take sum of rows   return(rsum_s)   } 

some of things did wrong:

  1. as.numeric , sample had no effect unless assign result, importantly
  2. the rowsums , return had moved end, outside for loop, otherwise function exit after processing first column.

the code above still not efficient because @ each iteration replacing whole x multiple times. recommend @ apply family of functions, like:

sim <- function(x) {     fun <- function(z) rank(sample(as.numeric(z)), ties.method = "min")     y   <- as.data.frame(lapply(x, process.one.col))     rownames(y) <- rownames(x)     rowsums(y) } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -