-1

I have a data with three variables. Patient ID as ID, Visit as Visit, and a third variable as Var. I have two visits and I would like to subset the IDs:

  1. IDs that have missing value of Var in the Visit 2
  2. IDs had a value 2 for Var in the Visit 1 but 1 in the Visit 2 and vice versa
ID Visit Var
1    1   2
1    2   2
2    1   1
2    2   2
3    1   2
3    2   1
4    1   2
4    2   NA  
AVA
  • 81
  • 4

1 Answers1

0

You can subset using [ and using == for equal, & for and, is.na to test for missing values.

unique(x$ID[x$Visit == 2 & is.na(x$Var)])
#[1] 4

intersect(unique(x$ID[!is.na(x$Var) & x$Var == 2 & x$Visit==1])
        , unique(x$ID[!is.na(x$Var) & x$Var == 1 & x$Visit==2]))
#[1] 3

Data:

x <- read.table(header=TRUE, text="ID Visit Var
1    1   2
1    2   2
2    1   1
2    2   2
3    1   2
3    2   1
4    1   2
4    2   NA")
GKi
  • 37,245
  • 2
  • 26
  • 48