合并R中缺少值的数据帧 [英] Merging data frames with missing values in R

查看:110
本文介绍了合并R中缺少值的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取数据框的代码:

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") 

我有两个数据框:

  frequency isoforms        type                                               
8         17        8 rat_ensembl                                               
9          8        9 rat_ensembl                                               
10        20       10 rat_ensembl  

   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   

我想结合这些转换成一个数据框架,还包括出现在 rat_all 数据框中的缺少
isoforms 条目但不是 rat_ensembl
数据帧。所以我希望输出是一个组合的数据框架,就像我在两个数据框中绑定
一样,但是增加了:

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            

然后,理论上我可以选择 isoforms frequency.x type.x 列和
修复它们,因此它们是正确的每个 rat_ensembl rat_all 然后 rbind 那些
数据帧在一起,但似乎应该有一些只是直接处理它。

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.

推荐答案

也许你想要这样的东西

z <- merge(rat_ensembl, rat_all, all = TRUE)

iso_diff <- setdiff(rat_all$isoforms, rat_ensembl$isoforms)

augmented <- data.frame(frequency = 0, isoforms = iso_diff, type = "rat_ensembl", stringsAsFactors= FALSE)

df_all <- rbind(z, augmented)

希望有所帮助。

这篇关于合并R中缺少值的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆