0

A key column between two dataframes:

df1 <- data.frame(id = c(1,2,3,4,5), quant = c(23,4,34,52,22))
df2 <- data.frame(tshape = c(43,3,5,1,6), id = c(1,2,3,4,5))

I used all options

dfall <- merge(x = df1, y = df2, by = "id", all = TRUE)
dfall <- merge(x = df1, y = df2, by = "id", all.y = TRUE)
dfall <- merge(x = df1, y = df2, by = "id", all.x = TRUE)
dfall2 <- merge(x = df1, y = df2, by = "id", all.x = TRUE)

but anyone give the result I try to extract:

id  quant tshape 
1    23     43
2     4      3
3    34      5
4    52      1
5    22      6
Elr Mant
  • 507
  • 1
  • 4
  • 14
  • `dfall <- merge(x = df1, y = df2, by = "id", all = TRUE)` Works for me. – Chabo Feb 08 '19 at 22:55
  • Maybe I am mistaken but I think you may be getting confused by the index/rownames. That is not an actual column but just part of the printing of a data frame. If you do not want to see those numbers `print(dfall, row.names=F)` – Chabo Feb 08 '19 at 23:00

1 Answers1

1

Running the following appears to give me the correct output:

df1 <- data.frame(id = c(1,2,3,4,5), quant = c(23,4,34,52,22))
df2 <- data.frame(tshape = c(43,3,5,1,6), id = c(1,2,3,4,5))
dfall <- merge(df1, df2, by = "id")

This gives output

>print(dfall, row.names=F)

id quant tshape
1    23     43
2     4      3
3    34      5
4    52      1
5    22      6
Chabo
  • 2,842
  • 3
  • 17
  • 32
sumshyftw
  • 1,111
  • 6
  • 14