0

I have two dfs with same sample names. However, the sample names in df1 repeat many times, while df2 is with only one time of sample names. For example, df1 is with 3 times of sample names, while df2 is with one time of sample names. Now I want to combine or fill the information of df2 into df1 for all the samples.

df1

       value
sample1 0.5
sample2 0.3
sample3 0.1
sample1 0.5
sample2 0.3
sample3 0.1
sample1 0.5
sample2 0.3
sample3 0.1

df2

        treatment   dose
sample1      a       1
sample2      b       2
sample3      c       3

The expected result should be like below:

        gene treatment dose
sample1  0.5    a       1
sample2  0.3    b       2
sample3  0.1    c       3
sample1  0.5    a       1
sample2  0.3    b       2
sample3  0.1    c       3
sample1  0.5    a       1
sample2  0.3    b       2
sample3  0.1    c       3

Thank you very much for your help.

Bio_farmer
  • 149
  • 1
  • 7

1 Answers1

0

We can merge on the row.names

merge(df1, df2, by = "row.names")

NOTE: Here, we assume the first dataset to be a matrix as data.frame don't allow duplicate row names

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you very much. The df1 is data frame, not matrix. What should I do? – Bio_farmer Jul 07 '19 at 16:48
  • @Bio_farmer Is that the row names. If it is a data.frame, it wouldn't allow to have duplicate names, but may convert to a unique name with `make.unique` – akrun Jul 07 '19 at 16:49
  • Hi, sorry in the example data, it is row names. However, it is not row names in the real data. I just merged them with by="sample" command. Thank you. – Bio_farmer Jul 07 '19 at 16:52
  • @Bio_farmer In that case, it is the way in which you showed the data is not correct. You should have used `dput` to show the example – akrun Jul 07 '19 at 16:52