-1

Let's say I have a first file with header "Model ID", "log(P)", "skewness", "acuteness", "mean mag", "err in mean mag", "A1", "A1_err", "phi1", "phi1_err", "R21", "R21_err", "R31", "R31_err", "P21", "P21_err", "P31", "P31_err" that looks like this:

1   -0.123  -0.306  inf 1.043   0.000   0.010   0.000   0.653   0.000   0.091   0.000   0.009   0.000   3.097   0.000   0.137   0.002
2   -0.142  -0.170  inf 1.035   0.000   0.064   0.000   0.538   0.000   0.560   0.000   0.289   0.000   3.168   0.000   6.182   0.000
3   -0.160  -0.143  inf 1.027   0.000   0.086   0.000   0.401   0.000   0.631   0.000   0.400   0.000   3.348   0.000   0.130   0.000
4   -0.176  -0.117  inf 1.020   0.000   0.107   0.000   0.249   0.000   0.592   0.000   0.435   0.000   3.526   0.000   0.402   0.001
...
7247    -0.210  -0.331  inf -0.276  0.000   0.264   0.000   0.243   0.000   0.064   0.000   0.063   0.000   1.500   0.001   1.710   0.001

and I have a second file that is smaller in row numbers that looks like this

Model                   z                   x                   M                   L                   T          Lin Period         Growth rate                logT                logP                logM
    2             0.00038             0.75053                 0.5                  35                6050             0.72127           0.0093333             8.70781           -0.326742           -0.693147
    3             0.00038             0.75053                 0.5                  35                6100             0.69281            0.015857             8.71604           -0.366999           -0.693147
    4             0.00038             0.75053                 0.5                  35                6150             0.66735            0.021103             8.72421           -0.404441           -0.693147
    5             0.00038             0.75053                 0.5                  35                6200             0.64414            0.024205              8.7323           -0.439839           -0.693147
...
 7247             0.00061             0.74996                 0.8                 105                6800             0.83493           0.0012547             8.82468           -0.180407           -0.223144

As you can tell, both files have Model ID in their first columns. I would like to combine horizontally the rows of the first file (except Model ID because that would be redundant) to the right of the second file by their Model ID. Is there a convenient method/tool to do this?

NOTE: The second file does NOT have the same number of rows as the first file, so I cannot simply use join/merge like I initially thought. Please download and check out the files to see for yourself.

Woj
  • 449
  • 1
  • 5
  • 15

1 Answers1

1
library(tidyverse)    
joined <- left_join(file2, file1, by = c(`Model ID`))

This will join rows from file1 that match file2 at Model ID onto the right of file2 without duplicating the Model ID column. Since file1 is bigger, rows in file1 with Model IDs that are missing from file2 will not be included in the result. If you want those, do the join in the opposite order and rows in file1 without a match in file2 will have NAs.

Elle
  • 998
  • 7
  • 12