[R] factors

David Winsemius dwinsemius at comcast.net
Fri May 6 00:37:43 CEST 2011


On May 5, 2011, at 6:28 PM, J wrote:

> Hi, I'm requesting you don't berate me for asking this question:
>
> I clearly don't have the gist of factors.
>
> I have two dataframes, A and B.
>
> Each of them has a column containing strings (they're labels).
>
> I want to, one-by-one in a loop, compare the particular string in an  
> entry from dataframe A to an entry in B, to see if they're the same.
>
> The problem, when posing the question:
> searchID1 <- A[['label']][i]
> possMatch1 <- B[['label']][j]
> searchID1 == possMatch1
>
> I get the error:
> Error in Ops.factor(searchID1, possMatch1) :
>  level sets of factors are different
>
> I presume this is because the set of possible values in the 'labels'  
> columns, respectively in A and B, differ.  In my case, I'm not  
> interested in this at all; I just want to compare individual entries  
> from the two dataframes in a pair-wise fashion.
>
> Can I strip the "factors" associated with the entries?  Is there a  
> better way?

Use as.character around the factors. Assuming they have equal nummbers  
of rows, then no loop is needed:

searchID1 <- as.character(A[['label']])
possMatch1 <- as.character(B[['label']])
searchID1 == possMatch1  # returns pair-wise logical vector

And if the number of rows are not equal, then you need to express your  
problem more clearly (and the dyadic function %in% is probably neededt)


-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list