我只是试图用几组相同的数据重复我几个月前所做的事情。 Comparing two dataframe in r
这些是我的数据框:
df1
> 1 2 3
> 1 AT GC CC
> 2 AG GC CT
> 3 GG TT <NA>
df2
> 1 2 3
> 1 AT <NA> GG
> 2 AG GC CG
> 3 GG TT AA
results I want
> 1 2 3
> 1 TRUE <NA> FALSE
> 2 TRUE TRUE FALSE
> 3 TRUE TRUE <NA>
我的两个dfs (df1,df2)
的级别不同,所以我需要将它们更改为字符。
A <- lapply(df1, as.character)
B <- lapply(df2, as.character)
所以我做比较:
A == B
但我得到一个错误:
Error in A == B :
comparison of these types is not implemented
STR(DF1): `
'data.frame': 82 obs. of 24 variables:
$ rs1 : Factor w/ 3 levels "AA","AC","CC": 2 3 3 3 3 1 2 3 3 2 ...
$ rs2 : Factor w/ 2 levels "TC","TT": 2 2 2 2 2 2 2 2 2 2 ...
$ rs3 : Factor w/ 2 levels "AG","GG": 2 2 2 2 2 2 1 2 2 2 ...
$ rs4 : Factor w/ 3 levels "CC","TC","TT": 1 1 1 1 1 2 1 1 1 1 ...
$ rs5 : Factor w/ 2 levels "TC","TT": 2 2 2 2 2 2 2 2 2 2 ...
$ rs6 : Factor w/ 3 levels "GG","TG","TT": 3 1 2 2 1 2 1 2 2 1 ...
$ rs7 : Factor w/ 3 levels "AA","AG","GG": 1 2 2 1 2 1 2 2 2 2 ...
$ rs8 : Factor w/ 3 levels "AA","AG","GG": 3 2 3 2 3 3 2 2 2 2 ...
$ rs9 : Factor w/ 3 levels "CC","CG","GG": 3 3 3 3 3 3 3 3 3 3 ...
$ rs10 : Factor w/ 2 levels "CC","TC": 1 1 1 1 1 1 1 1 2 1 ...
$ rs11 : Factor w/ 3 levels "GG","TG","TT": 1 2 1 1 2 1 1 1 2 1 ...
$ rs12 : Factor w/ 2 levels "AC","CC": 2 2 2 2 2 2 2 2 2 2 ...
$ rs13 : Factor w/ 2 levels "CC","TC": 1 1 1 1 1 1 1 1 1 1 ...
$ rs14 : Factor w/ 2 levels "CC","TC": 1 1 2 1 1 1 1 1 1 1 ...
$ rs15 : Factor w/ 2 levels "CG","GG": 2 2 2 2 2 2 2 2 2 2 ...
$ rs16: Factor w/ 2 levels "AC","CC": 2 2 1 2 2 2 2 2 2 2 ...
$ rs17 : Factor w/ 3 levels "AA","AG","GG": 3 3 3 3 3 3 3 1 3 3 ...
$ rs18 : Factor w/ 2 levels "AA","AG": 1 1 1 1 1 1 1 1 2 1 ...
$ rs19 : Factor w/ 3 levels "AA","AG","GG": 1 3 1 1 1 2 1 1 2 2 ...
$ rs20 : Factor w/ 2 levels "AA","AC": 1 1 1 1 1 1 1 1 1 1 ...
$ rs21: Factor w/ 1 level "CC": 1 1 1 1 1 1 1 1 1 1 ...
$ rs22 : Factor w/ 3 levels "AA","AC","CC": 2 2 2 2 3 2 2 3 3 1 ...
$ rs23 : Factor w/ 4 levels "A","AA","AC",..: 4 2 NA NA 2 2 2 3 3 2 ...
$ rs24 : Factor w/ 2 levels "AG","GG": 2 2 2 2 2 2 2 2 2 1 ...`
STR(DF2): `
'data.frame': 82 obs. of 24 variables:
$ rs1 : Factor w/ 3 levels "CC","AC","AA": 2 1 1 1 1 3 2 1 1 2 ...
$ rs12 : Factor w/ 2 levels "TC","TT": 2 2 2 2 2 2 2 2 2 2 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs3 : Factor w/ 2 levels "AG","GG": 2 2 2 2 2 2 1 2 2 2 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs4 : Factor w/ 3 levels "CC","TC","TT": 1 1 1 1 1 2 1 1 1 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs5 : Factor w/ 3 levels "TT","TC","CC": 1 1 1 1 1 1 1 1 1 1 ...
$ rs6 : Factor w/ 3 levels "GG","TG","TT": 3 1 2 2 1 2 1 2 2 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs7 : Factor w/ 3 levels "GG","AG","AA": 3 2 2 3 2 3 2 2 2 2 ...
$ rs8 : Factor w/ 3 levels "GG","AG","AA": 1 2 1 2 1 1 2 2 2 2 ...
$ rs9 : Factor w/ 3 levels "CC","CG","GG": 3 3 3 3 3 3 3 3 3 3 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs10 : Factor w/ 2 levels "TC","CC": 2 2 2 2 2 2 2 2 1 2 ...
$ rs11 : Factor w/ 3 levels "GG","TG","TT": 1 2 1 1 2 1 1 1 2 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs12 : Factor w/ 2 levels "AC","CC": 2 2 2 2 2 2 2 2 2 2 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs13 : Factor w/ 2 levels "CC","TC": 1 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs14 : Factor w/ 2 levels "CC","TC": 1 1 2 1 1 1 1 1 1 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs15 : Factor w/ 3 levels "AG","CG","GG": 3 3 3 3 3 3 3 3 3 3 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs16: Factor w/ 2 levels "AC","CC": 2 2 1 2 2 2 2 2 2 2 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs17 : Factor w/ 3 levels "AA","AG","GG": 3 3 3 3 3 3 3 1 3 3 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs18 : Factor w/ 2 levels "AG","AA": 2 2 2 2 2 2 2 2 1 2 ...
$ rs19 : Factor w/ 3 levels "AA","AG","GG": 1 3 1 1 1 2 1 1 2 2 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs20 : Factor w/ 3 levels "AA","AC","CC": 1 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs21: Factor w/ 1 level "CC": 1 1 1 NA 1 1 1 1 1 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs22 : Factor w/ 3 levels "AA","AC","CC": 2 2 2 1 3 2 2 3 3 1 ...
..- attr(*, "names")= chr "i001.p2" "i002.p2" "i003.p2" "i005.p2" ...
$ rs23 : Factor w/ 3 levels "CC","AC","AA": 1 3 2 2 3 3 3 2 2 3 ...
$ rs24 : Factor w/ 2 levels "GG","AG": 1 1 1 1 1 1 1 1 1 1 ...`