The following dataframe will illustrate the problem

 DF<-data.frame(name=rep(1:5,each=2),x1=rep("A",10),x2=seq(10,19,by=1),x3=rep(NA,10),x4=seq(20,29,by=1))
 DF$x3[5]<-50

 # we have a data frame. we are interested in the columns x2,x3,x4 which
contain sparse
 # values and many NA.
 DF
   name x1 x2 x3 x4
1     1  A 10 NA 20
2     1  A 11 NA 21
3     2  A 12 NA 22
4     2  A 13 NA 23
5     3  A 14 50 24
6     3  A 15 NA 25
7     4  A 16 NA 26
8     4  A 17 NA 27
9     5  A 18 NA 28
10    5  A 19 NA 29

# we have a list of "target values that we want to search for in the data
frame
# if the value is in the data frame we want to keep it there,  otherwise,
 replace it with NA

targets<-c(11,12,13,16,19,50,27,24,22,26)
# so we apply a test by column to the last 3 columns using the "in" test
# this gives us a mask of whether the data frame 'contains' elements in the
# target list

mask<-apply(DF[,3:5],2, "%in%" ,targets)
mask

         x2    x3    x4
 [1,] FALSE FALSE FALSE
 [2,]  TRUE FALSE FALSE
 [3,]  TRUE FALSE  TRUE
 [4,]  TRUE FALSE FALSE
 [5,] FALSE  TRUE  TRUE
 [6,] FALSE FALSE FALSE
 [7,]  TRUE FALSE  TRUE
 [8,] FALSE FALSE  TRUE
 [9,] FALSE FALSE FALSE
[10,]  TRUE FALSE FALSE

# and so DF[2,3] is equal to 11 and 11 is in the target list, so the mask is
True
# now something like DF<- ifelse(mask==T,DF,NA) is CONCEPTUALLY what I want
to do
in the end I'd  Like a result that looks like

   name x1 x2 x3 x4
1     1  A NA NA NA
2     1  A 11 NA NA
3     2  A 12 NA 22
4     2  A 13 NANA
5     3  A NA 50 24
6     3  A NA NA NA
7     4  A 16 NA 26
8     4  A NA NA 27
9     5  A NA NA NA
10    5  A 19 NA NA

Ive tried forcing the DF and the mask into vectors so that ifelse() would
work
and have tried "apply" using ifelse.. without much luck. any thoughts?

	[[alternative HTML version deleted]]

