I want to use R to match the content of my data frame it has two columns which look like:
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
I want to sort the two columns so like matches like, and any non-match is printed also
1B-73066 1B-73066
1B-73068 1B-73068
1B-73070 1B-73070
1B-73076 1B-73076
1C-1095 1C-1095
1C-80140
1C-80154
Thanks
This seems to do what you ask for, though I'm confused by your example:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.