Code to get the data frames:
rat_all = structure(list(frequency = c(37L, 31L, 14L, 11L, 2L, 3L), isoforms = 8:13,
type = structure(c("rat_all", "rat_all", "rat_all", "rat_all",
"rat_all", "rat_all"), .Dim = c(6L, 1L))), .Names = c("frequency",
"isoforms", "type"), row.names = 8:13, class = "data.frame")
rat_ensembl = structure(list(frequency = c(17L, 8L, 20L), isoforms = 8:10,
type = structure(c("rat_ensembl", "rat_ensembl", "rat_ensembl"
), .Dim = c(3L, 1L))), .Names = c("frequency", "isoforms",
"type"), row.names = 8:10, class = "data.frame")
I have two data frames:
frequency isoforms type
8 17 8 rat_ensembl
9 8 9 rat_ensembl
10 20 10 rat_ensembl
and
frequency isoforms type
8 37 8 rat_all
9 31 9 rat_all
10 14 10 rat_all
11 11 11 rat_all
12 2 12 rat_all
13 3 13 rat_all
I'd like to combine these into one data frame, but also to include the missing
isoforms
entries that appear in the rat_all
data frame but not the rat_ensembl
data frame. So I'd like the output to be a combined data frame as if I rbinded
the two data frames, but augmented with:
11 0 11 rat_ensembl
12 0 12 rat_ensembl
13 0 13 rat_ensembl
I thought I could do it with merge but I wind up getting a huge mess that I have to unwind that I can eventually massage into the right format but it is not a good solution if I wanted to do this for four or five different 'types' at once. What am I missing? Thanks!
To be clear I'm looking to get a final data frame that looks like:
frequency isoforms type
1 17 8 rat_ensembl
2 8 9 rat_ensembl
3 20 10 rat_ensembl
4 37 8 rat_all
5 31 9 rat_all
6 14 10 rat_all
7 11 11 rat_all
8 2 12 rat_all
9 3 13 rat_all
10 0 11 rat_ensembl
11 0 12 rat_ensembl
12 0 13 rat_ensembl
I can kind of get it to do what I want if I use:
z = merge(rat_ensembl, rat_all, by.x="isoforms", by.y="isoforms", all.y=TRUE)
isoforms frequency.x type.x frequency.y type.y
7 7 44 rat_ensembl 69 rat_all
8 8 17 rat_ensembl 37 rat_all
9 9 8 rat_ensembl 31 rat_all
10 10 20 rat_ensembl 14 rat_all
11 11 NA <NA> 11 rat_all
12 12 NA <NA> 2 rat_all
13 13 NA <NA> 3 rat_all
14 14 NA <NA> 1 rat_all
Then, theoretically I could select out the isoforms
, frequency.x
, type.x
columns and
fix them so they are correct for each of rat_ensembl
and rat_all
and then rbind
those
data frames together but it seems like there should be something to just handle it directly.