Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R cbind based on row names

Tags:

I have two data series vectors.

x = c("100"=2,"200"=2)
y = c("150"=3,"200"=2)

How can I merge them so that I get:

     x     y
100  2     NA
150  NA    3  
200  2     2

I tried simple merge but it doesn't give me NAs but overwrites them.

Edit: Following 'Akrun' suggestion....

In my more large case, when I did this, the rownames seems to have been re-organized incorrectly. Is there a way to make it so that the row names increase downwards? ...ie 550 starts first

 Row.names miv.adj.put miv.adj.call
1       1000   0.2314474    0.2300720
2       1010   0.2253973    0.2248859
3       1020   0.2197318    0.2192542
4       1030   0.2135290    0.2134169
5       1040   0.2079630    0.2072208
6       1050   0.2018332    0.2014297
7       1060   0.1964593    0.1960722
8       1070   0.1904258    0.1903321
9       1080   0.1846698    0.1848534
10      1090   0.1795015    0.1794104
11      1100   0.1741656    0.1740738
12      1110   0.1687536    0.1689416
13      1120   0.1636827    0.1635842
14      1130   0.1588984    0.1587930
15      1140   0.1544471    0.1546785
16      1150   0.1505388    0.1500152
17      1160   0.1467702    0.1466178
18      1170   0.1442214    0.1434934
19      1180   0.1417465    0.1415235
20      1190   0.1359671    0.1404349
21      1200   0.1304330    0.1388388
22      1210   0.1364605    0.1394886
23      1220   0.1384152    0.1387143
24      1230   0.1380744    0.1407685
25      1240   0.1402303    0.1421706
26      1250   0.1442225    0.1448018
27       550   0.5580139           NA
28       600   0.5013123           NA
29       650   0.4531128           NA
30       700   0.4305649           NA
31       710   0.4078400           NA
32       720   0.3991150           NA
33       730   0.3901917           NA
34       740   0.3839478           NA
35       750   0.3770905           NA
36       760   0.3741577    0.3157471
37       770   0.3660187    0.2912659
38       780   0.3611389    0.3264923
39       790   0.3553551    0.3091400
40       800   0.3488472    0.3093491
41       810   0.3442551    0.3220001
42       820   0.3386833    0.3101685
43       830   0.3343002    0.3174255
44       840   0.3253029    0.3097153
45       850   0.3193840    0.3055168
46       860   0.3134457    0.3083755
47       870   0.3060543    0.3028351
48       880   0.2993553    0.2925442
49       890   0.2958691    0.2904507
50       900   0.2900932    0.2857182
51       910   0.2838757    0.2817528
52       920   0.2784557    0.2748661
53       930   0.2720226    0.2695099
54       940   0.2660784    0.2637886
55       950   0.2603851    0.2582973
56       960   0.2547527    0.2528450
57       970   0.2490301    0.2472813
58       980   0.2430965    0.2414871
59       990   0.2368519    0.2358116
like image 732
user1234440 Avatar asked Mar 31 '16 09:03

user1234440


1 Answers

We can use merge

 merge(as.data.frame(x), as.data.frame(y), by='row.names', all=TRUE)
like image 136
akrun Avatar answered Oct 11 '22 12:10

akrun