2012-08-07 75 views
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 
+0

感謝ttmaccer指出和jon糾正錯誤 – SHRram 2012-08-07 22:14:34

回答

2

目前似乎是在genofun定義的錯誤,但一旦被固定在下面應該工作:

P1v<-as.character(pars$value[sdf$P1]) 
P2v<-as.character(pars$value[sdf$P2]) 
dum<-cbind(sdf,t(mapply(genofun,P1.v=P1v,P2.v=P2v,n=5))) 
names(dum)<-c('P1','P2','P1.v','P2.v','CLD1','CLD2','CLD3','CLD4','CLD5') 
> dum 
    P1 P2 P1.v P2.v CLD1 CLD2 CLD3 CLD4 CLD5 
6 1 7 AB AB AA AB AB AB AA 
28 4 8 BB BB BB BB BB BB BB 
27 4 7 BB AB AB BB AB BB AB 
43 8 9 BB AB AB BB BB BB AB 
36 6 7 AA AB AB AA AA AB AA 
26 4 6 BB AA AB AB AB AB AB 
1 1 2 AB BB AB AB AB AB AB 
9 1 10 AB AB AB BB AA AB AB 
>