<commit_before>---
layout: post
title: Manipulating NAs in longitudinal data
author: samclifford
tags:
 - r
 - dplyr
 - tidyr
---

This snippet was motivated by a hack that <NAME> presented to BRAG on 4 February 2016

Sometimes a longitudinal data set, e.g. patient information, doesn't have all subjects observed at all time points. Here's a way of generating an index of patient IDs corresponding to non-missing response values which may be useful for analysis with JAGS.

We'll make use of `tidyr` and `dplyr` to manipulate some simulated data.


``` r
library(tidyr)
library(dplyr)

# simulate some data
n.r <- 30 # how many subjects
n.c <- 4 # how many sequential observations

N <- n.r*n.c

X <- matrix(rnorm(N), nrow = n.r, ncol=n.c)

head(X)
```

    ##             [,1]        [,2]       [,3]        [,4]
    ## [1,] -0.11111305  0.06214172 -0.1669861  0.61818158
    ## [2,] -0.17479358  1.39212638 -1.0141225 -0.07955313
    ## [3,] -0.08041327 -0.02065511  0.1647441 -0.70910392
    ## [4,] -1.06330211  0.39322485 -0.7833457 -0.86897735
    ## [5,]  0.42257947 -0.51270806  2.6705473 -0.03004519
    ## [6,] -0.30504952  0.06193476 -0.7525571 -0.18959407

``` r
# knock out some values as being missing
X[sample(1:N, size = N/4)] <- NA

head(X)<commit_msg>everything gets a read more now due to excerpt chopping first paragraph<commit_after>---
layout: post
title: Manipulating NAs in longitudinal data
author: samclifford
tags:
 - r
 - dplyr
 - tidyr
---

This snippet was motivated by a hack that <NAME> presented to BRAG on 4 February 2016. Sometimes a longitudinal data set, e.g. patient information, doesn't have all subjects observed at all time points. Here's a way of generating an index of patient IDs corresponding to non-missing response values which may be useful for analysis with JAGS.

We'll make use of `tidyr` and `dplyr` to manipulate some simulated data.

<!---excerpt-break-->

``` r
library(tidyr)
library(dplyr)

# simulate some data
n.r <- 30 # how many subjects
n.c <- 4 # how many sequential observations

N <- n.r*n.c

X <- matrix(rnorm(N), nrow = n.r, ncol=n.c)

head(X)
```

    ##             [,1]        [,2]       [,3]        [,4]
    ## [1,] -0.11111305  0.06214172 -0.1669861  0.61818158
    ## [2,] -0.17479358  1.39212638 -1.0141225 -0.07955313
    ## [3,] -0.08041327 -0.02065511  0.1647441 -0.70910392
    ## [4,] -1.06330211  0.39322485 -0.7833457 -0.86897735
    ## [5,]  0.42257947 -0.51270806  2.6705473 -0.03004519
    ## [6,] -0.30504952  0.06193476 -0.7525571 -0.18959407

``` r
# knock out some values as being missing
X[sample(1:N, size = N/4)] <- NA

head(X)
{'fraction_non_alphanumeric': 0.13445378151260504, 'fraction_numerical': 0.17570664629488159, 'mean_word_length': 2.8685376661742983, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '371119', 'n_tokens_mistral': 1272, 'n_tokens_neox': 979, 'n_words': 321}