Home > OS >  R: Combining the Output of "Replicates"
R: Combining the Output of "Replicates"

Time:01-29

I am working with the R programming language.

I have the following function:

I have a function

fn <- function()
{ i = 1:100
v1 = rnorm(100,100,100)
v2 = rnorm(100,100,100)
out = data.frame(i, v1,v2)
return(out)
}

My Question: I want to use the replicate() function in R to run this function 100 times and combine the output of all 100 runs into a single output (i.e. 3 columns, 100x100 = 10000 rows):

output = replicate(100, fn())

When I do this, the data comes out in some other format:

   [,1]        [,2]        [,3]        [,4]        [,5]        [,6]        [,7]       
i  integer,100 integer,100 integer,100 integer,100 integer,100 integer,100 integer,100
v1 numeric,100 numeric,100 numeric,100 numeric,100 numeric,100 numeric,100 numeric,100
v2 numeric,100 numeric,100 numeric,100 numeric,100 numeric,100 numeric,100 numeric,100

I know that I can perform this exact same task with a FOR LOOP - but is there any way I can do this with the replicate() function?

In reality, I have a very large function which I don't want to modify with "i" and "j" indices - I thought perhaps I could place the replicate() into a loop itself?

my_list = list()

for ( i in 1:100)
{

out_i = replicate(fn(),1)
out_i$round = i
my_list[i] = out_i
}

final <- do.call(rbind.data.frame, my_list)

But I am not sure how to do this - can someone please show me how to do this?

Thanks!

CodePudding user response:

Use simplify=FALSE and then do.call(rbind, ..):

### does not work
output <- replicate(2, mtcars[1:2,])
output
#      [,1]      [,2]     
# mpg  numeric,2 numeric,2
# cyl  numeric,2 numeric,2
# disp numeric,2 numeric,2
# hp   numeric,2 numeric,2
# drat numeric,2 numeric,2
# wt   numeric,2 numeric,2
# qsec numeric,2 numeric,2
# vs   numeric,2 numeric,2
# am   numeric,2 numeric,2
# gear numeric,2 numeric,2
# carb numeric,2 numeric,2

### does work
output <- replicate(2, mtcars[1:2,], simplify = FALSE)
output
# [[1]]
#               mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
# [[2]]
#               mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
output <- do.call(rbind, output)
output
#                mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4       21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag   21   6  160 110  3.9 2.875 17.02  0  1    4    4
# Mazda RX41      21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag1  21   6  160 110  3.9 2.875 17.02  0  1    4    4

CodePudding user response:

I think I might have solved it myself?

my_list <- lapply(1:100, function(i) {
    out_i <- my_function()
    out_i$round <- i
    out_i
})

final <- do.call(rbind, my_list)

Is this correct?

  •  Tags:  
  • r
  • Related