0

When I use the merge function below, it removes the actual row names and replaces them with numbers

x<- merge(y$total, z$total, by="row.names", all=TRUE)

Is there a solution to this? Thanks

Row.names   y   z
1          1  47 149
2         10 157 107
3         11  97  74
4         12 116  78
5         13  52  96
6         14  17  22
7         15   0   0
8         16 120  88
9         17 128  94
10        18  33  69
11        19  44   6
12         2   0   0
13        20  32  32
14         3  73  67
15         4 112 164
16         5   7  24
17         6  53  21
18         7   4  20
19         8 150 144
20         9  34  21


James
  • 43
  • 3
  • 1
    Create a column with `row.names` in each dataset and `merge` by that column – akrun Dec 18 '19 at 18:09
  • 1
    What types of objects are `y$total` and `z$total`? Are you trying to merge data.frames or vectors? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 18 '19 at 18:35
  • From the help file: If the matching involved row names, an extra character column called Row.names is added at the left, and in all cases the result has ‘automatic’ row names. – manotheshark Dec 18 '19 at 18:43
  • You are likely not getting the results you want because of what you are passing to the function. The following do not produce the same data `y$total` and `y["total"]`; one is an array and another is a data.frame. – manotheshark Dec 18 '19 at 18:48

1 Answers1

3

The problem is that the code has not passed any row names to merge. It is passing it two vectors which have no names. You can either pass two vectors having names or two data frames having row names. We show the latter in the example below. Note that BOD$Time is a plain vector with no row names so we do not use that; however, BOD["Time"] is a one-column data frame with row names.

# create test data frame using built-in BOD data frame adding row names
rownames(BOD) <- paste0("x", 1:6)

# merge, set row names from Row.names column and remove that column
m <- merge(BOD["Time"], BOD["Time"], by = 0, all = TRUE)
rownames(m) <- m$Row.names
m$Row.names <- NULL

giving:

> m
   Time.x Time.y
x1      1      1
x2      2      2
x3      3      3
x4      4      4
x5      5      5
x6      7      7
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341