Does plyr skip missing levels of a factor [that is the grouping variable]? It's the first of my questions in diagnosing a problem.
I have a dataset where patients are in strata=rural
or strata=city
. I want to compare the age
in treatment=A
to treatment=B
.
For example, I am trying to do:
ddply(data.c, .(strata), function(x) t.test(age~treatment, data=x, na.rm=TRUE)
But it tells me
Error in t.test.formula(age ~ treatment, data = x, na.rm = TRUE) : grouping factor must have exactly 2 levels
If I run factor(data$strata)
and factor(data$treatment)
I see only two levels, respectively (they are two labels, respectively; that's not a problem, right?).
Does plyr think that NA is a level in the grouping factor? What might be the problem for the error message?
I've been Googling and looking on stackoverflow to answer the question. I'm pretty new to R but I could not find an answer. Any help is much appreciated.
So I have some example code. Chase's example worked nicely but when I use my data, I still get the same error.
dlply(data.c, .(strata), function(x) t.test(age~treatment, data=x, na.rm=TRUE
produced the same error.
I don't understand why this is.
Below is my data.c
strata age treatment
1 1 57.8630136986301 p_3
2 0 52.958904109589 p_3
3 NA 97.2438356164384 p12
4 0 88.2027397260274 p12
5 1 77.5890410958904 p12
6 1 43.9123287671233 p12
7 1 63.5260273972603 p_3
8 1 42.1890410958904 p12
9 0 52.1753424657534 p12
10 1 65.6493150684932 p12
11 0 44.9835616438356 p12
12 1 64.8849315068493 p12
13 1 57.5835616438356 p12
14 0 47.3013698630137 p12
15 0 74.0356164383562 NA
16 0 65.4986301369863 p12
17 1 83.986301369863 p12
18 0 47.4904109589041 p12
19 1 47.8630136986301 p12
20 1 58.8520547945205 p12
21 1 61.3342465753425 p12
22 1 66.841095890411 p12
23 1 55.6383561643836 p12
24 1 52.7178082191781 p12
25 1 71.4630136986301 p12
26 1 NA p12
27 1 59.2082191780822 p12
28 1 69.8575342465753 p12
29 1 46.7397260273973 p12
30 1 53.5013698630137 p_3
31 1 41.3205479452055 p12
32 0 51.3917808219178 p_3
33 1 47.8684931506849 p12
34 1 87.654794520548 p12
35 0 75.558904109589 p12
36 1 71.2520547945205 p12
37 1 44.9808219178082 p_3
38 1 52 p_3
39 1 54.7643835616438 p_3
40 1 85.6630136986301 p_3
41 1 74.1835616438356 p_3
42 1 56.8684931506849 p_3
43 1 87.572602739726 p_3
44 1 85.0109589041096 p_3
45 1 70.0767123287671 p_3
46 0 47.2328767123288 p12
47 1 63.7972602739726 p12
48 1 85.8054794520548 p12
49 1 67.027397260274 p12
50 1 60.7342465753425 p12
51 0 61.9479452054794 p_3
52 0 86.8712328767123 p12
53 1 87.8219178082192 p12
54 1 49.9424657534247 p12
55 0 83.386301369863 p12
56 1 88.3013698630137 p12
57 1 55.7890410958904 p12
58 1 63.7616438356164 p12
59 1 55.5041095890411 p12
60 1 43.5232876712329 p12
61 1 58.8246575342466 p12
62 0 46.7397260273973 p12
63 0 74.2027397260274 p_3
64 0 51.9205479452055 p_3
65 0 78.1890410958904 p_3
66 0 78.9917808219178 p_3