my df is shown below
mydf<- structure(list(IDs = c(11L, 16L, 19L, 21L, 22L, 24L, 42L, 43L,
51L), string1 = structure(c(1L, 8L, 7L, 2L, 4L, 9L, 6L, 3L, 5L
), .Label = c("b", "g", "hue", "hyu", "if", "jud", "ufhy", "uhgf;ffugf",
"uhgs"), class = "factor"), IDs.1 = c(4L, 11L, 16L, 19L, 20L,
22L, 29L, NA, NA), string2 = structure(c(2L, 3L, 8L, 7L, 4L,
5L, 6L, 1L, 1L), .Label = c("", "a", "b", "higf;hdugd", "hyu",
"inja", "ufhy", "uhgf;ffugf"), class = "factor")), .Names = c("IDs",
"string1", "IDs.1", "string2"), class = "data.frame", row.names = c(NA,
-9L))
I want to get them together like below
myout<- structure(list(Ids = c(4L, 11L, 16L, 19L, 20L, 21L, 22L, 24L,
29L, 42L, 43L, 51L), string = structure(c(1L, 2L, 11L, 10L, 4L,
3L, 6L, 12L, 8L, 9L, 5L, 7L), .Label = c("a", "b", "g", "higf;hdugd",
"hue", "hyu", "if", "inja", "jud", "ufhy", "uhgf;ffugf", "uhgs"
), class = "factor")), .Names = c("Ids", "string"), class = "data.frame", row.names = c(NA,
-12L))
I tried to do it using merge
df1 <- mydf[,1:2]
df2 <- mydf[,3:4]
df3 = merge(df1, df2, by.x=c("IDs", "string"))
which gives me an error because they are unequal
I also tried to use the approach given here How to join (merge) data frames (inner, outer, left, right)? which did not solve my problem
my input is like this
IDs string1 IDs string2
11 b 4 a
16 uhgf;ffugf 11 b
19 ufhy 16 uhgf;ffugf
21 g 19 ufhy
22 hyu 20 higf;hdugd
24 uhgs 22 hyu
42 jud 29 inja
43 hue
51 if
and the output looks like this
Ids string
4 a
11 b
16 uhgf;ffugf
19 ufhy
20 higf;hdugd
21 g
22 hyu
24 uhgs
29 inja
42 jud
43 hue
51 if
e.g. 11, 16 etc are repeated twice , so we only want them once