1
我必須通過採樣過程生成數據並應用函數。在r中應用函數
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
要應用的每個組合的值在表格分析中。例如,對於第一個P1和P2組合1 =「AB」,7 =「AB」。其次,4 =「BB」,8 =「BB」。
現在我想將以下函數應用於sdf(考慮在表格中的值)。 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
感謝ttmaccer指出和jon糾正錯誤 – SHRram 2012-08-07 22:14:34