2016-05-03 35 views
0

我有超过200列的data.frame,并包括以下包括有关这个问题列一个子集:独特的组合,基于标准从一行

>df 
Variant Pos  ID DB.0.count DB.1.count sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9 sample10 
variant5 1234567 A  5    5    1/0  1/0  1/0  1/1  1/1  0/0  1/0  0/0  1/0  1/1 
.   .  .  .    .    F1   F1   F1   F2   F2   F3   F4   F4   F4   F5 

我想:

1.使samples1-sample10列,其中每个组合包含来自每个F数一个样品,即,每个组合包含5个样品从F1,F2,F3,F4,F5每一个样品的所有可能的组合。

所以在上面的实例中会有18点的组合,例如:

第一组合将是SAMPLE1,sample4,sample6,sample7,sample10

第二组合是SAMPLE1,sample4,sample6 ,样品8,sample10

第三组合是SAMPLE1,sample4,sample6,sample9,sample10

我与uniqueduplicated和0123发挥各地阅读相关帖子后,却没有任何地方。

然后,我想输出每个独特的组合到一个新的data.frame,对样本中的样本中的每个变量执行计数,并将结果输出到新列,然后执行下面的Fisher精确测试并输出到新列,下面,将下面的代码应努力做到:(费代码在这里了解到:Fisher's exact test on values from large dataframe and bypassing errors

df.combo.1$pop.0/0.count <- apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("0/0",u))==TRUE))  
df.combo.1$pop.1/0.count <- apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("1/0",u))==TRUE)) 
df.combo.1$pop.1/1.count <- apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("1/1",u))==TRUE)) 

df.combo.1$pop.0.count <- (2*(apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("0/0",u))==TRUE))) + apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("0/1",u))==TRUE))) 
df.combo.1$pop.1.count <- (2*(apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("1/1",u))==TRUE))) + apply(df.combo.1[,6:10], 1, function(u) length(which(grepl("0/1",u))==TRUE))) 

res <- NULL 
for (i in 1:nrow(df.combo.1)){ 
table <- matrix(c(df.combo.1[i, 4], df.combo.1[i, 5], df.combo.1[i, 14], df.combo.1[i, 15]), ncol = 2, byrow = TRUE) 
# if any NA occurs in your table save an error in p else run the fisher test 
if(any(is.na(table))) p <- "error" else p <- fisher.test(table)$p.value 
# save all p values in a vector 
res <- c(res,p) 
} 
df.combo.1$fishers <- res 


>df.combo.1 
Variant Pos  ID DB.0.count DB.1.count sample1 sample4 sample6 sample7 sample10 pop.0/0.count pop.1/0.count pop.1/1.count pop.0.count pop.1.count  fishers 
variant5 1234567 A  5    5    1/0  1/1  0/0  1/0  1/1  1    2    2    4    6    1.0000 
.   .  .  .    .    F1   F2   F3   F4   F5 

2.最后,我想创建一个data.frame,其中列出了每一个独特的组合Fisher精确p值如下:

>new.df 
combo fishers 
1  1.0000 
2  1.0000 
3  1.0000 
4  1.0000 
etc 

我认为这整个练习可能需要某种for循环?

回答

1

我想我已经掌握了你想要的东西。对于我认为你在第1部分中挣扎的那部分,我使用了其中的组合和expand.grid来整理。

对于第2部分来说,一旦数据按照每个观察基准排列在1行上,该部分就是一个相当容易的分组。

它看起来像你每个观察使用2行(除非这只是一个格式化的东西),这使得它非常困难(但不是不可能,只需要更多的杂耍),所以我已经将数据组合到一行中。这应该是一个非常简单的转换,只需将每个“第二”行中的相应列附加到每个“第一”行,然后删除每一行。

这可以做得更有效率和整洁,但我认为这是有效的,应该相当容易地扩展到其他情况。

问候, 乔希

# provided demo data 
# Variant Pos  ID DB.0.count DB.1.count sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9 sample10 
# variant5 1234567 A  5    5    1/0  1/0  1/0  1/1  1/1  0/0  1/0  0/0  1/0  1/1 
# .   .  .  .    .    F1   F1   F1   F2   F2   F3   F4   F4   F4   F5 


# create data frame in long format 
test.df <- as.data.frame(t(c("variant5",1234567,"A",5,5,"1/0","1/0","1/0","1/1","1/1","0/0","1/0","0/0","1/0","1/1","F1", "F1", "F1", "F2", "F2", "F3", "F4", "F4", "F4", "F5"))) 
# ensure as character format 
test.df[] <- lapply(test.df, as.character) 
# get postions of "F" data 
F1.var <- which(test.df =="F1") 
F2.var <- which(test.df =="F2") 
F3.var <- which(test.df =="F3") 
F4.var <- which(test.df =="F4") 
F5.var <- which(test.df =="F5") 
# get all combinations of the 5 F positions 
Fcode.combinations <- expand.grid(F1.var,F2.var,F3.var,F4.var,F5.var) 
# create results data frame 
df.combo.1 <- as.data.frame(matrix(NA,ncol = 21, nrow = nrow(Fcode.combinations))) 
# name variables 
names(df.combo.1) <- c("Variant","Pos","ID","DB.0.count","DB.1.count", 
           "F1.sample.pos","F1.result", 
           "F2.sample.pos","F2.result", 
           "F3.sample.pos","F3.result", 
           "F4.sample.pos","F4.result", 
           "F5.sample.pos","F5.result", 
           "pop.0_0.count","pop.1_0.count","pop.1_1.count", 
           "pop.0.count","pop.1.count", 
           "fishers") 
# copy in common data 
df.combo.1[,1:5] <- test.df[,1:5] 
# setup variables based on combination data 
for(i in 1:nrow(Fcode.combinations)){ 
    df.combo.1[i,c(6,8,10,12,14)] <- Fcode.combinations[i,] 
    # -10 to correct for the position of the results not the 'F type' data 
    cycle.results <- as.numeric(Fcode.combinations[i,] -10) 
    df.combo.1[i,c(7,9,11,13,15)] <- test.df[cycle.results] 
} 

# this is essentially your code with the column reference changed 

df.combo.1$pop.0_0.count <- apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("0/0",u))==TRUE))  
df.combo.1$pop.1_0.count <- apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("1/0",u))==TRUE)) 
df.combo.1$pop.1_1.count <- apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("1/1",u))==TRUE)) 

df.combo.1$pop.0.count <- (2*(apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("0/0",u))==TRUE))) + apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("0/1",u))==TRUE))) 
df.combo.1$pop.1.count <- (2*(apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("1/1",u))==TRUE))) + apply(df.combo.1[,c(7,9,11,13,15)], 1, function(u) length(which(grepl("0/1",u))==TRUE))) 

res <- NULL 
for (i in 1:nrow(df.combo.1)){ 
    table <- matrix(as.numeric(c(df.combo.1[i, 4], df.combo.1[i, 5], df.combo.1[i, 16], df.combo.1[i, 17])), ncol = 2, byrow = TRUE) 
    # if any NA occurs in your table save an error in p else run the fisher test 
    if(any(is.na(table))) p <- "error" else p <- fisher.test(table)$p.value 
    # save all p values in a vector 
    res <- c(res,p) 
} 
df.combo.1$fishers <- res 

# create results data 
df.combo.1.results <- as.data.frame(cbind(1:nrow(df.combo.1),df.combo.1$fishers)) 
names(df.combo.1.results) <- c("combo","fishers") 
+0

完美,太感谢你了! – emily