Reputation: 40
I have a data frame of two columns
set.seed(120)
df <- data.frame(m1 = runif(500,1,30),n1 = round(runif(500,10,25),0))
and I wish to add a third column that uses column n1
and m1
to generate a normal distribution and then to get the standard deviation of that normal distribution. I mean to use the values in each row of the columns n1
as the number of replicates (n) and m1
as the mean.
How can I write a function to do this? I have tried to use apply
stdev <- function(x,y) sd(rnorm(n1,m1))
df$Sim <- apply(df,1,stdev)
But this does not work. Any pointers would be much appreciated.
Many thanks, Matt
Upvotes: 1
Views: 1065
Reputation: 73385
Your data frame input looks like:
# > head(df)
# m1 n1
# 1 12.365323 15
# 2 4.654487 15
# 3 10.993779 24
# 4 24.069388 22
# 5 6.684450 18
# 6 15.056766 16
I mean to use the values in each row of the columns n1 and m1 as the number of replicates (n) and as the mean.
First show you how to use apply
:
apply(df, 1, function(x) sd(rnorm(n = x[2], mean = x[1])))
But a better way is to use mapply
:
mapply(function(x,y) sd(rnorm(n = x, mean = y)), df$n1, df$m1)
apply
is ideal for matrix input; for data frame input you get great overhead for type conversion.
Upvotes: 1