合并data.frames而不更改列类

时间:2016-03-23 19:24:52

标签: r merge

我正在尝试合并到data.frames,其中每个df都有数字列:

> head(tpm)
      FUS3_1NM_NO HOG1_1NM_HS IRE1_1NM_HS YAK1_NO_NO    WT_NO_NO  WT_1NM_NO
Q0010  0.00000000 0.000000000 0.000000000 0.00000000 0.000000000 0.00000000
Q0017  0.00000000 0.000000000 0.000000000 0.00000000 0.000000000 0.00000000
Q0032  0.00000000 0.000000000 0.000000000 0.00000000 0.000000000 0.00000000
Q0045  0.01480327 0.008769000 0.005630685 0.07375834 0.016947766 0.05605933
Q0050  0.01901093 0.000000000 0.006622705 0.11939809 0.011356401 0.05999602
Q0055  0.01642850 0.005019137 0.003222851 0.07277538 0.005526441 0.02537609

> head(fpkm)
         WT.basal WT.1NM.PP1 Atg1.AS.1NM.PP1 Fus3.AS.1NM.PP1 Hog1.AS.1NM.PP1
YAL001C   15.8848    21.4220         22.1730         23.9904         21.2069
YAL002W   30.7453    40.4043         45.3739         48.5827         46.2439
YAL003W 3919.8700  2505.7200       2707.6900       2757.9900       2582.3100
YAL007C  444.3640   494.6290        456.1560        489.1200        451.0880
YAL008W   90.1188   136.8680        128.3930        150.6090        160.5000
YAL009W   75.1799    73.7186         78.1866         88.3664         88.5497

我想在row.name上合并这些表:

all = merge(tpm,fpkm,by=0,all=T)

> head(all)
  Row.names FUS3_1NM_NO HOG1_1NM_HS IRE1_1NM_HS YAK1_NO_NO    WT_NO_NO
1     Q0010  0.00000000 0.000000000 0.000000000 0.00000000 0.000000000
2     Q0017  0.00000000 0.000000000 0.000000000 0.00000000 0.000000000
3     Q0032  0.00000000 0.000000000 0.000000000 0.00000000 0.000000000
4     Q0045  0.01480327 0.008769000 0.005630685 0.07375834 0.016947766
5     Q0050  0.01901093 0.000000000 0.006622705 0.11939809 0.011356401
6     Q0055  0.01642850 0.005019137 0.003222851 0.07277538 0.005526441

但是,生成的data.frame有一个名为Row.names的附加列,并且已将所有列的类从numeric更改为character。

我必须将它们转换回数字,然后删除Row.names列。

有没有办法在row.name上进行合并,不包含row.name列,并保持data.frame的类相同(数字)

1 个答案:

答案 0 :(得分:0)

很遗憾,当by = 0时,您无法绕过Row.names列,请尝试以下方法:How does one merge dataframes by row name without adding a "Row.names" column?

> all = transform(merge(tpm,fpkm,by=0,all = T), row.names = Row.names, Row.names = NULL)

这只是重命名所有行,并删除行列。