我想生成一个包含三列(A,B和C)的大型数据框(100000行和3列)。
此数据框架满足以下两个条件:
A+B+C=1
; 我无法弄清楚如何生成这种数据集。
非常感谢你提前提出的建议。
Songchao
答案 0 :(得分:1)
N <- 100000
library(triangle)
A <- rtriangle(N, 0.2, 0.4, 0.3)
B <- rtriangle(N, 0.3, 0.5, 0.4)
C <- 1 - A - B
d = data.frame(A, B, C)
summary(d)
nr <- 100000
u1 <- runif(nr)
u2 <- runif(nr)
u3 <- (2 - u1 - u2) / 2
U <- cbind(u1, u2, u3)
# shuffle, because I am not sure about the tails of u3
for (i in (1:nrow(U))) {
U[i, ] <- U[i, sample(1:3)]
}
t1 <- qtriangle(U[, 1], 0.2, 0.4, 0.3)
t2 <- qtriangle(U[, 2], 0.3, 0.5, 0.4)
t3 <- qtriangle(U[, 3], 0.1, 0.5, 0.3)
d <- cbind(t1, t2, t3)
summary(d)
cor(d)
答案 1 :(得分:0)
我不确定这是否有效,因为我不确定转换是否会导致分发失败,但请尝试这样做:
install.packages("triangle") #if not already present
library(triangle)
a <- rtriangle(10, a = .2, b = .4)
b <- rtriangle(10, a = .3, b = .5)
c <- rtriangle(10, a = .1, b = .5)
m <- cbind(a, b, c)
test <- sweep(m, 1, rowSums(m), FUN = "/") #divide all rows by their rowSums
> test
a b c
[1,] 0.3237202 0.4034106 0.2728692
[2,] 0.2419613 0.3821729 0.3758658
[3,] 0.2476927 0.3721925 0.3801149
[4,] 0.2983462 0.4254064 0.2762474
[5,] 0.3427140 0.4830743 0.1742117
[6,] 0.2456610 0.3306648 0.4236742
[7,] 0.3189454 0.4148087 0.2662459
[8,] 0.3400111 0.3770924 0.2828965
[9,] 0.3142197 0.3807667 0.3050136
[10,] 0.3221066 0.4222530 0.2556405
> rowSums(test)
[1] 1 1 1 1 1 1 1 1 1 1