Matt G
Matt G

Reputation: 40

Add column to dataframe which is the sd of rnorm from previous columns

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

Answers (2)

user2100721
user2100721

Reputation: 3597

Another option

lapply(Map(rnorm,n=df$m1,mean=df$n1),sd)

Upvotes: 0

Zheyuan Li
Zheyuan Li

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

Related Questions