我想使用R来匹配我的数据框的内容,它有两列,如下所示:
1B-73066 1C-80140
1B-73068 1C-80154
1B-73070 1D-21647
1B-73076 1D-21648
1C-1095 1B-73066
1C-1096 1B-73068
1C-14080 1B-73070
1C-1582 1B-73076
我想对两列进行排序,就像匹配一样,并打印任何不匹配
1B-73066 1B-73066
1B-73068 1B-73068
1B-73070 1B-73070
1B-73076 1B-73076
1C-1095 1C-1095
1C-80140
1C-80154
由于
答案 0 :(得分:0)
这似乎符合你的要求,虽然我对你的例子感到困惑:
df <- read.table(text="1B-73066 1C-80140
+ 1B-73068 1C-80154
+ 1B-73070 1D-21647
+ 1B-73076 1D-21648
+ 1C-1095 1B-73066
+ 1C-1096 1B-73068
+ 1C-14080 1B-73070
+ 1C-1582 1B-73076",stringsAsFactors = FALSE)
>
> names(df) <- c("A","B")
> df2 <- data.frame(
+ A = c(intersect(df$A,df$B),setdiff(df$A,df$B),rep("",length(setdiff(df$B,df$A)))),
+ B = c(intersect(df$A,df$B),rep("",length(setdiff(df$A,df$B))),setdiff(df$B,df$A)))
# > df2
# A B
# 1 1B-73066 1B-73066
# 2 1B-73068 1B-73068
# 3 1B-73070 1B-73070
# 4 1B-73076 1B-73076
# 5 1C-1095
# 6 1C-1096
# 7 1C-14080
# 8 1C-1582
# 9 1C-80140
# 10 1C-80154
# 11 1D-21647
# 12 1D-21648