Home > Back-end >  How I create new dataset in R using the HELPmiss dataset
How I create new dataset in R using the HELPmiss dataset

Time:03-20

Using the HELPmiss dataset, I am only interested in patients that were randomized to a HELP clinic (treat = yes). How do I create a new dataset of just those people?

CodePudding user response:

You can use the filter function to select only patients with treat = yes, using this code:

library(mosaicData)
library(tidyverse)

data(HELPmiss)

your_data <- HELPmiss %>%
  filter(treat == "yes")

The data looks like this:

   age anysub cesd d1 daysanysub dayslink drugrisk e2b female    sex g1b homeless i1  i2 id indtot link      mcs      pcs
1   37    yes   49  3        177      225        0  NA      0   male yes   housed 13  26  1     39  yes 25.11199 58.41369
2   37    yes   30 22          2       NA        0  NA      0   male yes homeless 56  62  2     43 <NA> 26.67031 36.03694
3   47    yes    6  1         31      365        0  NA      1 female  no   housed  4   4  6     29   no 55.50899 46.47521
4   28    yes   32  1         47      365        7   8      0   male yes homeless 12  24  8     44   no  9.16053 65.13801
5   39    yes   46  4        115      382       20   3      0   male  no homeless 20  27 10     44   no 36.14376 22.61060
6   34   <NA>   46  0         NA      365        8  NA      1 female  no   housed  0   0 11     34   no 43.97468 60.07915
7   60    yes   36 10          6       22        0   1      0   male  no homeless 13  20 15     41  yes 25.84616 31.82965
8   36    yes   43  2          0      443        0  NA      0   male  no   housed 51  51 16     38   no 23.60844 55.16998
9   28    yes   35  6         27       41        0   2      1 female yes homeless  0   0 17     26  yes 29.79983 44.77651
10  27     no   52  0        198       49       10   4      1 female yes   housed  9  24 20     37  yes 15.45827 37.45214
11  41   <NA>   35  1         NA      391       12   1      0   male  no   housed 26  26 22     36   no 20.87145 36.58481
12  33    yes   18  1        129      272        0  NA      0   male  no   housed  0   0 23     27  yes 47.28674 61.64098
13  34    yes   30  1        154       56        0  NA      0   male  no   housed  3   3 28     34  yes 37.37156 63.06006
14  35    yes   27  0         34      361        1  NA      0   male  no homeless  7   7 30     37   no 34.33567 61.82597
15  29    yes   47  1        142       79        0   3      0   male  no homeless  0   0 32     37  yes 27.71771 42.22490
16  37     no   11  0        203      203        3  NA      0   male  no homeless  6   6 35     35  yes 27.85261 63.52000
17  29     no   26  1        193      354        0  NA      0   male  no   housed  0   0 36     21   no 54.77435 53.35109
18  33    yes   29  1         10       29        0  NA      0   male  no   housed  0   0 37     30  yes 27.49548 56.73985
19  20    yes   34  1        177      365        0  NA      0   male  no homeless 32 135 38     33   no 56.32433 53.23396
20  18   <NA>   38  1         NA      365        0   1      1 female  no homeless 24  32 41     36   no 25.19557 34.28825
21  43     no   16 15        191      414        0  NA      0   male  no homeless 24  36 44     41   no 15.86192 71.39259
22  28    yes   36  1         31      414        0  NA      0   male  no homeless  6  12 45     39   no 24.14882 52.61977
23  42    yes   36  2         17       38        7  NA      0   male  no   housed 13  13 47     39  yes 29.41298 50.06427
24  34    yes    5  2         23       14        0  NA      1 female  no   housed  6  13 50      8  yes 59.45409 52.69898
25  44   <NA>   36  5         NA      321       19   1      0   male yes homeless 15  26 52     42   no 29.39028 40.38438
26  30     no   44  2        209       26       21   2      0   male yes homeless  9  15 54     44  yes 17.92525 45.48341
27  37    yes   29  2        111       18        0  NA      0   male  no homeless  5  13 56     40  yes 34.43470 63.05807
28  35    yes   46  3         17      365        0  NA      1 female  no   housed 13  20 57     32   no 24.00031 46.75086
29  44    yes   44  1          4       27        0  NA      0   male yes   housed  3   6 59     44  yes 26.65304 40.46056
30  38    yes   30  5         18       30        0   2      0   male  no homeless 36  36 61     38  yes 26.06578 47.60514
31  41     no   29  3        181       19        0   2      1 female yes   housed  3   6 65     20  yes 33.37417 55.23372
32  35    yes   28  1         36      400        0   1      0   male  no   housed 32  32 67     38   no 35.83964 52.68871
33  40     no   57  5        181       34        0  NA      1 female yes homeless 59 164 71     43  yes 17.70596 36.04016
34  38   <NA>   26  4         NA      133        1  NA      0   male  no   housed  0   0 72     38  yes 39.93416 53.15686
35  42    yes   31  2        103       48        8   3      0   male  no homeless 26  51 73     44  yes 23.99673 45.18499
   pss_fr  racegrp satreat sexrisk substance treat avg_drinks max_drinks hospitalizations
1       0    black      no       4   cocaine   yes         13         26                3
2       1    white      no       7   alcohol   yes         56         62               22
3       5    black      no       5   cocaine   yes          4          4                1
4       4    white     yes       6   alcohol   yes         12         24                1
5       0    white     yes       0    heroin   yes         20         27                4
6       0    white      no       2    heroin   yes          0          0                0
7       1    black      no       4   cocaine   yes         13         20               10
8       1    white      no       8   alcohol   yes         51         51                2
9       7 hispanic     yes       3    heroin   yes          0          0                6
10     13    white      no       3    heroin   yes          9         24                0
11      8    black      no       4    heroin   yes         26         26                1
12     14    black      no       4   cocaine   yes          0          0                1
13      3    white      no       5   cocaine   yes          3          3                1
14      6    black      no       4    heroin   yes          7          7                0
15      5    black     yes       2   cocaine   yes          0          0                1
16      2    black     yes       5   cocaine   yes          6          6                0
17     10    black      no       2   cocaine   yes          0          0                1
18     10    black      no       0   cocaine   yes          0          0                1
19      8    black      no       3   alcohol   yes         32        135                1
20      8    other      no       3   missing   yes         24         32                1
21      3    white      no       7   cocaine   yes         24         36               15
22      4    black      no       7   cocaine   yes          6         12                1
23     14    white      no       4    heroin   yes         13         13                2
24     12    black      no       4   cocaine   yes          6         13                2
25     11    black      no      10    heroin   yes         15         26                5
26      6    other      no       9    heroin   yes          9         15                2
27      2    black      no       7   alcohol   yes          5         13                2
28      1    black      no       7   cocaine   yes         13         20                3
29     13    other      no       4   cocaine   yes          3          6                1
30     10    black      no       4   alcohol   yes         36         36                5
31     13    white     yes       4   alcohol   yes          3          6                3
32     12    black     yes       6   cocaine   yes         32         32                1
33      1    black      no       4   alcohol   yes         59        164                5
34      8    white     yes       2    heroin   yes          0          0                4
35      3    white     yes       6   alcohol   yes         26         51                2

When you check the treat column you will see only yes.

  •  Tags:  
  • r
  • Related