Skip to content
Prev 277567 / 398502 Next

create list of names where two df contain == values

On Nov 16, 2011, at 8:03 AM, Rob Griffin wrote:

            
When you are building a helper function for use with apply, your  
should realize that tat function will be getting a vector, not a list.  
The construction "[[,a]]" looks pretty strange as well. Generally  
column selection is done with one of "[[a]]" or "[ , a]". I am not  
absolutely sure that you cannot have "[[,]]" but I was under the  
impression you could not. AND you shouldn't be retruning NULLs if what  
yoyr really want are NA's.
So a single vector will be assigned to the x argument in the ify  
function and the rest of the arguments will be populated from the  
other arguments. You do NOT need to supply an "x" argument in that  
list and if you do so you will throw an error.

Furthermore you cannot expect the apply function to keep track of  
which row it's one for indexing a different data.frame. The mapply  
function might be used for this purpose but I am going to suggest a  
much cleaner solution below.
If you know that df.1 and df.2 have the same number of rows then use  
the ifelse function which is designed to work on vectors. The if)_else  
construct is NOT:

 > ifelse( df.1[,2] ==df.2[,3], {as.character(df.1[,1])} ,  {NA} )
  [1] "a" "b" "c" "d" "e" "f" "g" NA  "i" "j"

The reason as.character was needed lies in that fact that you  
constructed df.1[,1] as a factor variable. AS I understand it, the  
ifelse tries to make it numeric to match the datatype of the  
comaprison. I've never understood this frankly. Maybe someoen can  
educate me.

If you wanted a function that allowed you to specify the columns and  
dataframes then consider this

ret3.m1.eq.n2 <- function(df1, df2, col1, col2, col3){
                 ifelse( df1[,col1] ==df2[,col2],  
{as.character(df1[,col3])} ,  {NA} )
David Winsemius, MD
West Hartford, CT