I am trying to create a new column on my data.frame (df
) with values of Weibull densities.
I need to assign densities for vector of quantiles (w) taking into account groups in this df.
The subsets are delimited by the variable "plot".
Each plot has specific Weibull parameters in which the densities should be originated from.
The parameters are stored in df_2
.
Reproducible example:
set.seed(25)
w = rweibull(1200,10,28)
plot = data.frame(c(rep.int(1,300),rep.int(2,300),rep.int(3,300),rep.int(4,300)))
names(plot)[1] = c("plot")
df = cbind(plot,w)
df_2=data.frame(cbind(c(1,2,3,4),c(28,27,26,25),c(9,9.5,8,7)))
names(df_2)[1:3] = c("plot","scale","shape")
I tried to adapt a code from hadley's answer here, but did not succeed.
library(plyr)
weibull_density <- ddply(df, "plot", function(x) {
data.frame(
density = dweibull(df$w, scale=df_2$scale, shape=df_2$shape)
)
})
nrow(weibull_density)
[1] 4800
It returns a data.frame with 4800 rows (I was expecting 1200).
I also took a look in examples provided in ?ddply
help page, but could not figure out how to adapt it to this situation.
You obtain 4 values because
df_2
has 4 rows. You need to tellR
to use, e.g., the first row ofdf_2
for thew
values forplot == 1
.The following code produces the expected output: