I wan to get the mean of dat after 10 times repeated loops.
I tried with the following code.
df=function(x){
dat=rnorm(x)
dat
}
replicate(n = 10, expr = {
df(x=5)
})
Thanks
I wan to get the mean of dat after 10 times repeated loops.
I tried with the following code.
df=function(x){
dat=rnorm(x)
dat
}
replicate(n = 10, expr = {
df(x=5)
})
Thanks
# avoid df as an object name, as an object of that name
# is always in the namespace, and some operations interpret
# the name as a closure
mk_df <- function(x) {
dat <- rnorm(x)
dat
}
apply(replicate(n = 10, expr = mk_df(5)), 2,mean)
#> [1] 0.38782911 0.77414331 0.61026993 -1.00028940 0.11886385 0.48070768
#> [7] -0.07799182 -0.85150985 -0.27631772 -0.11024515
Created on 2020-09-30 by the reprex package (v0.3.0.9001)
Thanks @technocrat for the additional info.
I am expecting output should be 5 only, value should be a mean of repeated 10 times.
Is that one mean of 10 replications of one data frame of a five-element vector?
yes, it is. I want to replicate 10 times and took their mean of respective rows.
means of ten rows of 5 element vectors
rep(mean(mk_df(5)),10)
[1] -0.0457907 -0.0457907 -0.0457907 -0.0457907 -0.0457907 -0.0457907
[7] -0.0457907 -0.0457907 -0.0457907 -0.0457907
Thanks for your time and effort @technocrat.
I did not clearly mention the output. As attached in the picture, I want to replicate 10 times and take their mean of rows as highlighted.
mk_df <- function(x,y) {
dat <- rnorm(x)
rep(dat,10) -> a
dim(a) <- c(x,y)
a
}
rowMeans(mk_df(5,10))
#> [1] 0.9131058 0.9089604 0.3837234 -0.2396053 0.3903397
Created on 2020-10-01 by the reprex package (v0.3.0.9001)
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.