In R I get “Ops.factor(left, right): level sets of factors are different” when comparing 2 data frames

徘徊边缘 提交于 2019-12-11 10:17:13

问题


I have 2 data frames (A and B, consisting of 5 columns each) created from 2 .csv files. I want to compare 2 columns (A1_maj and A2_min) from A with 2 columns from B, which consist in values of the kind A, T, C, G. This is an example:

A:                         B:

A1_maj  A2_min              A1_maj  A2_min
C       T                   C       T                   
C       T                   C       T 
G       A                   G       A
G       A                   A       G
T       C                   G       A

I have a long code but I am stuck with a command. Whenever I try to compare columns from A and B using the following logic statements I get an error message:

Logic statements:

A$A1_maj == B$A2_min
A$A1_maj != B$A2_min

Error message:

Error in Ops.factor(A$A1_maj, B$A2_min) :
level sets of factors are different

On the other hand, when I include logic statements like this (see below) I get a proper output (TRUE or FALSE values):

A$A1_maj != B$A1_maj

I read in previous posts that I should include "stringsAsFactors=FALSE" when I create the data frame, since I might have converted character vectors into factors, but this is not making any difference

Any suggestion would be welcome !


回答1:


The solution, as suggested by @Roland, is to convert all factors from A and B into characters:

i <- sapply(A, is.factor)
A[i] <- lapply(A[i], as.character)

i <- sapply(B, is.factor)
B[i] <- lapply(B[i], as.character)


来源:https://stackoverflow.com/questions/35774173/in-r-i-get-ops-factorleft-right-level-sets-of-factors-are-different-when-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!