Home > Net >  Splitting a long list into 2 separate groups in RStudio
Splitting a long list into 2 separate groups in RStudio

Time:11-28

I have a data of 199 test subjects (125 exposed subjects & 74 unexposed subjects). Each subject has 3 different points attached to them. I need to:

  1. Split the data into 2 groups (the exposed group & the unexposed group).
  2. Find the average of the 3 points for each subject
  3. Find the average of the averages for both groups
The data:

    ...1     babyStatus oneMin fiveMin tenMin

1      1   ZIKV-exposed      6       7      9
2      2   ZIKV-exposed      4       8      8
3      3   ZIKV-exposed      4       9      8
4      4   ZIKV-exposed      8       9      7
5      5   ZIKV-exposed      7       7      7
6      6   ZIKV-exposed     10      10      7
7      7   ZIKV-exposed      9       6      7
8      8   ZIKV-exposed      8       7      6
9      9   ZIKV-exposed      4       6      8
10    10   ZIKV-exposed      1       7      9
11    11   ZIKV-exposed      4       8      7
12    12   ZIKV-exposed      2       6     10
13    13   ZIKV-exposed      5       9      7
14    14   ZIKV-exposed      7       6      7
15    15   ZIKV-exposed      6       8      7
16    16   ZIKV-exposed      2      10      9
17    17   ZIKV-exposed      8       5      7
18    18   ZIKV-exposed      7       8     10
19    19   ZIKV-exposed      8       7      8
20    20   ZIKV-exposed      8       9      8
21    21   ZIKV-exposed      9       8     10
22    22   ZIKV-exposed      4      10      8
23    23   ZIKV-exposed      9       6      6
24    24   ZIKV-exposed      5       8      8
25    25   ZIKV-exposed      6      10      8
26    26   ZIKV-exposed      3       8      8
27    27   ZIKV-exposed     10      10     10
28    28   ZIKV-exposed      3       7      7
29    29   ZIKV-exposed      6       9      8
30    30   ZIKV-exposed      9       6      7
31    31   ZIKV-exposed      7       6      8
32    32   ZIKV-exposed      7       6      8
33    33   ZIKV-exposed      7       9      9
34    34   ZIKV-exposed      4       7      9
35    35   ZIKV-exposed      3       7      6
36    36   ZIKV-exposed      2       6      8
37    37   ZIKV-exposed      3       8      8
38    38   ZIKV-exposed      2       9     10
39    39   ZIKV-exposed      1       9      6
40    40   ZIKV-exposed      7       7      6
41    41   ZIKV-exposed      5       8      8
42    42   ZIKV-exposed      3       9      9
43    43   ZIKV-exposed      9       9      7
44    44   ZIKV-exposed      8       9      7
45    45   ZIKV-exposed      5       6      9
46    46   ZIKV-exposed      4       8      7
47    47   ZIKV-exposed      3       5      7
48    48   ZIKV-exposed      5      10      6
49    49   ZIKV-exposed      3       7      6
50    50   ZIKV-exposed      6       9     10
51    51   ZIKV-exposed      4       9      9
52    52   ZIKV-exposed      3       8      9
53    53   ZIKV-exposed      8       7      8
54    54   ZIKV-exposed      4      10      9
55    55   ZIKV-exposed      8       6      8
56    56   ZIKV-exposed      8       8      8
57    57   ZIKV-exposed      5       6      9
58    58   ZIKV-exposed      1       5      8
59    59   ZIKV-exposed      4       8      8
60    60   ZIKV-exposed      2       7      8
61    61   ZIKV-exposed      3       8      7
62    62   ZIKV-exposed      9       7     10
63    63   ZIKV-exposed      6       6      6
64    64   ZIKV-exposed      8       8      9
65    65   ZIKV-exposed      6       7      7
66    66   ZIKV-exposed      3      10      8
67    67   ZIKV-exposed      9       7      7
68    68   ZIKV-exposed      2      10      6
69    69   ZIKV-exposed     10       5      8
70    70   ZIKV-exposed      8       9      7
71    71   ZIKV-exposed      5      10      8
72    72   ZIKV-exposed      3       7      7
73    73   ZIKV-exposed      1       8      7
74    74   ZIKV-exposed      3       6      6
75    75   ZIKV-exposed      2       5      7
76    76   ZIKV-exposed      6       9      6
77    77   ZIKV-exposed      2       7     10
78    78   ZIKV-exposed      3       7      7
79    79   ZIKV-exposed      5       9     10
80    80   ZIKV-exposed      2       9      6
81    81   ZIKV-exposed     10       6      7
82    82   ZIKV-exposed      2       9     10
83    83   ZIKV-exposed      2       5      9
84    84   ZIKV-exposed      4       6      7
85    85   ZIKV-exposed      6       8      9
86    86   ZIKV-exposed      5       8      9
87    87   ZIKV-exposed      6       9      8
88    88   ZIKV-exposed      7       7     10
89    89   ZIKV-exposed      8       9      9
90    90   ZIKV-exposed      8       6      7
91    91   ZIKV-exposed      6       6      7
92    92   ZIKV-exposed      5       6      9
93    93   ZIKV-exposed      6       6      8
94    94   ZIKV-exposed      8       7     10
95    95   ZIKV-exposed      5       9      6
96    96   ZIKV-exposed      9       7     10
97    97   ZIKV-exposed     10       6      7
98    98   ZIKV-exposed      1       8      9
99    99   ZIKV-exposed      7       7      9
100  100   ZIKV-exposed      3       8      7
101  101   ZIKV-exposed      3       7      7
102  102   ZIKV-exposed      7       6      8
103  103   ZIKV-exposed      4       5      9
104  104   ZIKV-exposed      4       8      6
105  105   ZIKV-exposed      4       7      7
106  106   ZIKV-exposed      3       8      6
107  107   ZIKV-exposed      9       9      7
108  108   ZIKV-exposed      4       5      9
109  109   ZIKV-exposed      4       6     10
110  110   ZIKV-exposed      9       5      8
111  111   ZIKV-exposed      6      10      6
112  112   ZIKV-exposed      1       9      7
113  113   ZIKV-exposed      8       6      8
114  114   ZIKV-exposed      8       9      6
115  115   ZIKV-exposed      2       9      7
116  116   ZIKV-exposed      7       7      7
117  117   ZIKV-exposed      6       7      6
118  118   ZIKV-exposed      2       6     10
119  119   ZIKV-exposed      6       8      7
120  120   ZIKV-exposed      2       7      8
121  121   ZIKV-exposed      5      10      8
122  122   ZIKV-exposed      5       7      9
123  123   ZIKV-exposed      5       9     10
124  124   ZIKV-exposed     10       8      8
125  125   ZIKV-exposed      9       8      9
126  126 ZIKV-unexposed      8       9     10
127  127 ZIKV-unexposed      9      10     10
128  128 ZIKV-unexposed      7      10     10
129  129 ZIKV-unexposed      7      10      9
130  130 ZIKV-unexposed     10      10      9
131  131 ZIKV-unexposed      9      10      9
132  132 ZIKV-unexposed      9      10      9
133  133 ZIKV-unexposed      7      10     10
134  134 ZIKV-unexposed      8      10      9
135  135 ZIKV-unexposed      8      10      9
136  136 ZIKV-unexposed      9       9      9
137  137 ZIKV-unexposed     10      10     10
138  138 ZIKV-unexposed      7      10     10
139  139 ZIKV-unexposed     10       9     10
140  140 ZIKV-unexposed      7       9     10
141  141 ZIKV-unexposed     10       9      9
142  142 ZIKV-unexposed      6      10      9
143  143 ZIKV-unexposed     10      10      9
144  144 ZIKV-unexposed      7      10      9
145  145 ZIKV-unexposed      6       9      9
146  146 ZIKV-unexposed      6      10      9
147  147 ZIKV-unexposed      6      10      9
148  148 ZIKV-unexposed      7       9      9
149  149 ZIKV-unexposed      6      10      9
150  150 ZIKV-unexposed      9       9      9
151  151 ZIKV-unexposed      9      10      9
152  152 ZIKV-unexposed      8      10     10
153  153 ZIKV-unexposed      8      10      9
154  154 ZIKV-unexposed      7      10      9
155  155 ZIKV-unexposed     10       9      9
156  156 ZIKV-unexposed      8      10     10
157  157 ZIKV-unexposed      8       9      9
158  158 ZIKV-unexposed     10      10      9
159  159 ZIKV-unexposed      8      10     10
160  160 ZIKV-unexposed      8      10      9
161  161 ZIKV-unexposed      9       9     10
162  162 ZIKV-unexposed      7      10     10
163  163 ZIKV-unexposed      8      10     10
164  164 ZIKV-unexposed     10       9      9
165  165 ZIKV-unexposed      8       9      9
166  166 ZIKV-unexposed      7       9     10
167  167 ZIKV-unexposed      9      10     10
168  168 ZIKV-unexposed      8      10     10
169  169 ZIKV-unexposed      6       9      9
170  170 ZIKV-unexposed      8      10     10
171  171 ZIKV-unexposed     10      10     10
172  172 ZIKV-unexposed      7      10      9
173  173 ZIKV-unexposed     10       9      9
174  174 ZIKV-unexposed      7      10     10
175  175 ZIKV-unexposed      8      10     10
176  176 ZIKV-unexposed      9       9     10
177  177 ZIKV-unexposed      8       9      9
178  178 ZIKV-unexposed      7       9      9
179  179 ZIKV-unexposed      8       9     10
180  180 ZIKV-unexposed      8       9     10
181  181 ZIKV-unexposed      8      10     10
182  182 ZIKV-unexposed      7      10      9
183  183 ZIKV-unexposed     10       9     10
184  184 ZIKV-unexposed     10      10      9
185  185 ZIKV-unexposed      9       9      9
186  186 ZIKV-unexposed      9      10      9
187  187 ZIKV-unexposed      8      10      9
188  188 ZIKV-unexposed      8       9     10
189  189 ZIKV-unexposed      9       9      9
190  190 ZIKV-unexposed      9       9     10
191  191 ZIKV-unexposed      8      10      9
192  192 ZIKV-unexposed     10       9      9
193  193 ZIKV-unexposed      8      10     10
194  194 ZIKV-unexposed      6      10      9
195  195 ZIKV-unexposed      8       9     10
196  196 ZIKV-unexposed      7      10      9
197  197 ZIKV-unexposed      7      10      9
198  198 ZIKV-unexposed      7      10      9
199  199 ZIKV-unexposed      8       9      9

The final result I need:

ZIKV-exposed: 6.928000
ZIKV-unexposed: 9.040541

enter image description here

CodePudding user response:

library(dplyr)

mydf |> 
  rowwise() |> 
  mutate(avg = mean(c_across(ends_with("in")))) |> 
  group_by(babyStatus) |> 
  summarize(avg = mean(avg))

# A tibble: 2 × 2
  babyStatus       avg
  <chr>          <dbl>
1 ZIKV-exposed    6.93
2 ZIKV-unexposed  9.04

CodePudding user response:

mean_table <- your_df %>%
# create a table so you have the mean for each ZIKV
transmute(babyStatus, Mean = rowMeans(select(.,oneMin:tenMin))) %>%
# subdivide class ZIKV in exposed/unexposed
group_by(babyStatus) %>%
# you can calculate mean in 'Mean' column, since its class is numeric, for each ZIKV
summarise(across(where(is.numeric), ~ mean(.x)))

print(mean_table)

# A tibble: 2 × 2
babyStatus      Mean
  <chr>          <dbl>
1 ZIKV-exposed    6.93
2 ZIKV-unexposed  9.04
  • Related