Q:

sapply with function using multiple columns as input

I have a data frame with let's say 2 columns and 4 rows (it's bigger... I am just making it simpler) like this: 
Value: 0.2, 0.3, 0.5, 0.8
R: 1, 0, 1, 0
I m trying to write a sapply line that given the value of R returns Value when R is 0 and NA when R is 1.  
I would write something like that: 
sapply(dataframe[[R]], function(x) ifelse(x==1,NA,dataframe[[Value]][[indexOf(x)]]))
Is there something like my "indexOf" (imaginary function) function that i could use? 
The task would be very easy using for loops but I want to do it efficiently with sapply.  

A:

ifelse is what you're looking for. 
> df = data.frame(Value=c(0.2, 0.3, 0.5, 0.8),
                  R=c(1, 0, 1, 0))

> df$New = ifelse(df$R==1, df$Value, NA)

> df
  Value R New
1   0.2 1 0.2
2   0.3 0  NA
3   0.5 1 0.5
4   0.8 0 NA 

