2017-06-29 49 views
1

我讀this SO questionthat one,但仍然無法解決我的問題。我有以下data.table,其中只包括我的data.table的總列和行的幾個。R:wilcoxon測試錯誤:分組因子必須有2個水平

library(data.table) 
structure(list(Patient = c("MB108", "MB108", "MB108", "MB108", 
"MB108", "MB108", "MB108", "MB108", "MB108", "MB108"), Visit = c(1, 
1, 1, 1, 9, 9, 9, 9, 12, 12), Stimulation = c("NC", "SEB", "PPD", 
"E6C10", "NC", "SEB", "PPD", "E6C10", "NC", "SEB"), `CD38 ` = c(83.3, 
63.4, 83.2, 91.5, 90.9, 70.9, 71, 88.4, 41.7, 47.9)), .Names = c("Patient", 
"Visit", "Stimulation", "CD38 "), class = c("data.table", "data.frame" 
), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x102806578>) 

我想這樣做在列4 t.test訪問時爲1,且當訪問是9. 我檢查對於NAS以及兩列的長度。

感謝您的幫助!

 #na.omit(boolean_dt3) 
     #print(length(unlist(boolean_dt3[Visit== 1,4, with = FALSE]))) 
     #print(length(unlist(boolean_dt3[Visit== 9,4, with = FALSE]))) 

wilcox.test(unlist(boolean_dt3[Visit== 1,4, with = FALSE])~ unlist(boolean_dt3[Visit== 9,4, with = FALSE]) , paired = T, correct=FALSE) 

回答

1

下面是如何在column 4分組由Value

library(dplyr) 
wilcox.test(filter(df, Visit==1)$CD38, filter(df, Visit==9)$CD38, paired=TRUE) 
+0

我不想使用的列名,因爲我通常不知道它。我也在運行多列測試,並將使用一個循環來運行所有測試。 – Rivka

+0

聽起來像你會很難在所有列上自動運行它。每列只能有2個因子,每列不適合測試。 – CPak

+0

爲什麼不呢?他們都是數字列。我想在訪問== 1並訪問== 9的人身上運行測試,並在我看到的2 – Rivka

4

我剛剛想出的,代替~作品我的問題進行Wilcoxon檢驗。

0

試試這個配方:

wilcox.test(numeric_var ~ two_level_group_var) 
+0

這並沒有提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/17849734) – QBrute