使用基于颜色的设置均值和方差生成数据集

时间:2016-03-11 20:08:56

标签: r data-generation

我有一个这个数据集structure(list(Color = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("blue", "green", "red" ), class = "factor")), .Names = "Color", row.names = c(NA, -30L ), class = "data.frame")

我想添加一个列,平均值为5,方差为2,平均值为10,方差为4,绿色,平均值为100,方差为15,红色..

1 个答案:

答案 0 :(得分:0)

如果您的数据按照示例中的顺序排序,请为means和std.deviations创建向量。然后使用Map为每个组调用rnorm

mus <- c(5, 10, 100)
sds <- c(2, 4, 15)
nums <- Map(function(n,mu, sd) rnorm(n, mu, sd), table(df1$Color), mus, sds)
df1$rndm <- unlist(nums)
df1
# Color       rndm
# 1   blue   5.048143
# 2   blue   8.159234
# 3   blue   7.029401
# 4   blue   3.298584
# 5   blue   3.388559
# 6   blue   3.125807
# 7   blue   3.428520
# 8   blue   3.659469
# 9   blue   7.881609
# 10  blue   3.483098
# 11 green   5.942954
# 12 green   3.172638
# 13 green  10.872443 

<强>更新

无需排序即可将最后一行更改为:

df1[order(df1$Color),"rndm"] <- unlist(nums)