-1

For each subject, one observation (or row) is kept based on the following:

if var2=c and var3=B then keep that observation. if not, check if var2=c and var3=S and keep that observation. if not, then then check if var2=L and var3=B, finally, check if var2=L and var3=S , if not put 0.

Any help would be appreciated.

My data looks like this:

id   var1   var2    var3
1   100 L   S
1   100 L   B
1   2   C   B
1   2   C   S
2   5   C   S
2   10  L   S
2   NA  L   B
2   NA  C   B

My desired result is:

id   var1   var2    var3
1   2   C   B
2   5   C   S
Hack-R
  • 19,705
  • 11
  • 63
  • 110
sri
  • 1
  • 2

1 Answers1

0

Here is an idea using dplyr,

library(dplyr)
x <- c('CB', 'CS', 'LB', 'LS') #vector with conditions
df %>% 
  group_by(id) %>% 
  na.omit() %>% 
  slice(order(match(paste0(var2, var3), x))[1])

#Source: local data frame [2 x 4]
#Groups: id [2]

#     id  var1   var2   var3
#  <int> <int> <fctr> <fctr>
#1     1     2      C      B
#2     2     5      C      S

DATA

dput(df)
structure(list(id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), var1 = c(100L, 
100L, 2L, 2L, 5L, 10L, NA, NA), var2 = structure(c(2L, 2L, 1L, 
1L, 1L, 2L, 2L, 1L), .Label = c("C", "L"), class = "factor"), 
    var3 = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c("B", 
    "S"), class = "factor")), .Names = c("id", "var1", "var2", 
"var3"), class = "data.frame", row.names = c(NA, -8L))
Sotos
  • 44,023
  • 5
  • 28
  • 55