2011-03-29 56 views
2

我寫了一個使用ggplot2生成圖的腳本,並且在每個圖中都有多個x軸值,並且它們中的每一個在y軸上有多個值,這個軸。我將以另一種方式提出問題:我有一個數據框中的多個子集,在for循環中生成,我如何控制for的循環以便生成另一個包含在每個數據框中的數據框行(以前的數據幀的第一列的值)對ggplot2中的特定數據執行統計測試

for (x in phy) { 
    print(x) 

    test<-subset(t, Phylum==x) 
    dat <- melt(test, measure=c("A","C","G","T","(A-T)/(A+T)","(G-C)/(G+T)", 
           "(A+T)/(G+C)")) 
    unitest <- unique(c(test$Class)) 
    #print(nrow(test)) 
    i <- 1 
    for(y in unitest) { 
     towork <- subset(test, Class==y) 

     # here i want to create a data frame that will contain (in each row, the 
     # value of the first column of the towork subset for each y) 

     # atest=wilcox.test(towork$A,towork$A, correct=FALSE) 
     # print(paste(paste(y,towork$A),towork$A)) 
    } 
} 



input: 

    e.g 
    class1: 
    0.268912 0.158921 0.214082 0.358085 
    1.680946   0.314681 0.210526 0.166895 
    0.286945 0.322006 0.147361 0.243688 
    class2 
    0.293873 0.327516 0.156235 0.222376  
    0.327430 0.308667 0.135710 0.227695  
    0.301488 0.326511 0.125865 0.246022  
    0.310980 0.308730 0.148861 0.231429 

我想新的數據幀在每一行中包含每個類的第一列。

output 
    e.g 
    1st row: 0.268912 1.680946 0.286945 
    2nd row:0.293873 0.327430 0.301488 0.310980 

等... ,然後包含在每個其他數據幀的行的每個類 等的第2列...

比我想在每2行的執行統計測試新的數據框架(例如Wilcoxon Rank Sum Test)並獲得結果。

任何幫助將提前意識到

Hello , i came up with an idea , but i need your help to do it. 
first the data is in a large text file and i will upload it if you want , my idea is : create a function that take 2 argument : 
1.the name of the column which should be used for grouping the data (e.g. phylum, or class) 
2. the name of the column containing the data to test (e.g. A,C,G,T) 
and i will test the data for each phylum first , and if i want i will test it for each class in each phylum. 
that's mean,i will take the A column for first phylum and A column for 2nd phylum and make the wilcox.test on them , and i will make the process for each common column in each phylum. and then i will use a subset function to test the classes inside each phylum. 
give me your opininon with this ?? 

日Thnx。

+0

@abd - 樣品輸入數據和預期輸出。使用'dput'來運行代碼所需的任何對象。 – Chase 2011-03-30 13:12:23

+0

@蔡斯:謝謝你的回覆。 每個y包含以下varaibles:ACGT 如0.268912 \t 0.158921 \t 0.214082 \t 0.358085 \t -0.142223 \t 0.096408 \t 1.680946 0.314681 0.210526 \t \t 0.166895 \t 0.307898 \t 0.010895 – weblover 2011-03-30 13:41:20

+0

@Chase:感謝您的回覆,我剛纔編輯的問題,並忘記我以前的評論,在此先感謝 – weblover 2011-03-30 13:53:01

回答

0

我認爲這會做你以後的事情。我們不一定需要完成爲感興趣的四個變量創建新數據框架的過程 - 我們可以從class1class2之內的各自位置提取感興趣的列。代碼已更新以查找class1和class2之間的公共列。它只會計算那些常見列的wilcox測試。

class1 <- matrix(rnorm(12), ncol = 4) 
class2 <- matrix(rnorm(16), ncol = 4) 

computeWilcox <- function(x, y, correct = FALSE, ...) { 

    if (!is.numeric(x)) stop("x must be numeric.") 
    if (!is.numeric(y)) stop("y must be numeric.") 

    commonCols <- intersect(colnames(x), colnames(y)) 

    ret <- vector("list", length(commonCols)) 

    for (col in 1:length(commonCols)) { 
     ret[[col]] <- wilcox.test(x[, col], y[, col], correct = correct, ...) 
    } 

    names(ret) <- commonCols 
    return(ret) 
} 


zz <- computeWilcox(class1, class2) 

其中ZZ具有如下的結構:

> str(zz) 
List of 2 
$ c:List of 7 
    ..$ statistic : Named num 0 
    .. ..- attr(*, "names")= chr "W" 
    ..$ parameter : NULL 
    ..$ p.value : num 0.0571 
    ..$ null.value : Named num 0 
    .. ..- attr(*, "names")= chr "location shift" 
    ..$ alternative: chr "two.sided" 
    ..$ method  : chr "Wilcoxon rank sum test" 
    ..$ data.name : chr "x[, col] and y[, col]" 
    ..- attr(*, "class")= chr "htest" 
$ d:List of 7 
    ..$ statistic : Named num 2 
    .. ..- attr(*, "names")= chr "W" 
    ..$ parameter : NULL 
    ..$ p.value : num 0.229 
    ..$ null.value : Named num 0 
    .. ..- attr(*, "names")= chr "location shift" 
    ..$ alternative: chr "two.sided" 
    ..$ method  : chr "Wilcoxon rank sum test" 
    ..$ data.name : chr "x[, col] and y[, col]" 
    ..- attr(*, "class")= chr "htest" 

您可以提取參數或p值出返回列表對象是這樣的:

> zz$c$p.value 
[1] 0.05714286 
+0

@Chase:你好,我想從中提取數據的類是動態地改變每個門(在第一個)soo每個門有不同數量的類,我想要做的是wilcox.test將分別應用於每個門的2個類別(第1和第2,第3和第4,...),並在圖表內顯示結果pdf。這是可以做到的嗎?謝謝 – weblover 2011-03-31 12:27:45

+0

@Chase:你好,這就是我想要的,但仍然存在1件事,我應該如何自動化測試每個門的類的過程?因爲每個門都有一個以上的類,每個類有4列(A,C,G,T),我想從每個門一起測試每個類的每個類(例如A列),每個類(例如G列)每個門的每個2類共同在一起,如何做到這一點?並抱歉煩惱:S – weblover 2011-03-31 12:57:09

+0

@abd - 這取決於數據來自何處,以及如何在工作環境中構建數據。一切都在一個大數據框架中嗎?很多小數據框架?一個包含data.frames的列表對象?解決方案可能需要調用「應用」或某種分組功能,但是如果沒有您的數據的代表性例子,就不可能說出。如果您回到我的第一條評論,並且「輸入」您嘗試使用的任何對象的內容 - 這將是最簡單的前進方式。 'dput'將允許其他人簡單地將對象複製並粘貼到R – Chase 2011-03-31 13:26:01

相關問題