3

I read this SO question and that one, but still could not solve my problem. I have the following data.table which includes only a few of my total columns and rows of my data.table.

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>)

I would like to do a t.test on column 4 when visit is 1 and when visit is 9. I checked for NAs as well as the length of both columns.

Thanks for any help!

      #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)
Rivka
  • 307
  • 1
  • 5
  • 19

3 Answers3

6

I just figured out , instead of ~ works for my problem.

Rivka
  • 307
  • 1
  • 5
  • 19
1

Here's how to perform wilcoxon test on column 4 grouping by Value

 library(dplyr)
 wilcox.test( filter(df, Visit==1)$CD38, filter(df, Visit==9)$CD38, paired=TRUE)
CPak
  • 13,260
  • 3
  • 30
  • 48
  • I don't want to use the column names, since I usually don't know it. I am also running the test for multiple columns and will use a loop to run the test on all of them. – Rivka Jun 29 '17 at 14:45
  • Sounds like you'll have a difficult time running this automatically on all columns. Each column can only have 2 factors, and each column won't be amenable for testing. – CPak Jun 29 '17 at 14:50
  • why not? they are all numeric columns. I want to run the test alway on those with visit ==1 and visit ==9 and devide one column in 2 – Rivka Jun 29 '17 at 14:56
  • I see. I misunderstood what you meant by `multiple columns` – CPak Jun 29 '17 at 14:59
0

try this formulation:

wilcox.test(numeric_var ~ two_level_group_var)
CcMango
  • 377
  • 1
  • 4
  • 15