我必须通过采样过程生成数据并应用一个函数。
p = 10; f = 8
set.seed (123)
pars <- data.frame (id = 1:p,
value = sample (c("AA", "AB", "AB", "BB"),p, replace = TRUE))
pars
id value
1 1 AB
2 2 BB
3 3 AB
4 4 BB
5 5 BB
6 6 AA
7 7 AB
8 8 BB
9 9 AB
10 10 AB
fdat <- data.frame (t(combn(pars$id,2)))
set.seed (1234)
sdf <- fdat[sample(1:nrow(fdat), f),]
names (sdf) <- c("P1", "P2")
sdf
P1 P2
6 1 7
28 4 8
27 4 7
43 8 9
36 6 7
26 4 6
1 1 2
9 1 10
要应用的每个组合的值都在表 pars 中。例如,对于第一个 P1 和 P2 组合 1 = "AB",7 = "AB"。其次,4 =“BB”,8 =“BB”。
现在我想将以下函数应用于 sdf(考虑 pars 表中的值)。P2.v 是 P1 的值,P2.v 是 P2 的值。
genofun <- function (P1.v, P2.v, n) {
if (P1.v == "AA" & P2.v == "BB" ) {
CLD <- rep ("AB", n)
}
if (P1.v == "BB" & P2.v == "AA" ) {
CLD <- rep ("AB", n)
}
if (P1.v == "AA" & P2.v == "AB") {
CLD <- sample (c("AA", "AB"), n, replace = TRUE)
}
if (P1.v == "AB" & P2.v == "AA") {
CLD <- sample (c("AA", "AB"), n, replace = TRUE)
}
if (P1.v == "BB" & P2.v == "AB") {
CLD <- sample (c("BB", "AB"), n, replace = TRUE)
}
if ( P1.v == "AB" & P2.v == "BB") {
CLD <- sample (c("BB", "AB"), n, replace = TRUE)
}
if (P1.v == "BB" & P2.v == "BB") {
CLD <- rep("BB", n, replace = TRUE)
}
if (P1.v == "AA" & P2.v == "AA") {
CLD <- rep("AA", n)
}
if (P1.v == "AB" & P2.v == "AB") {
CLD <- sample(c("AA", "AB","AB", "BB"), n, replace = TRUE)
}
out <- c(P1.v, P2.v, CLD)
return (out)
}
n = 5
genofun (P1, P2, n)
因此预期的输出将是:
P1 P2 P1.v P2.v CLD1 CLD2 CLD3 CLD4 CLD5 <- (essentially number columns = n)
6 1 7
28 4 8
27 4 7
43 8 9
36 6 7
26 4 6
1 1 2
9 1 10