0

I would like to add column "ID" from df2 to df1 as the first column. Can I do this in dplyr?

    df1     
    var1 var2 var3
    10   57    16
    8    66    34
    7    88    5
    1    90    94


    df2         
    ID  varX varY   varZ
    1   2      58   9
    2   10     69   4
    3   73     89   52
    4   14    100   9

Expected outcome:

    df1         
    ID  var1    var2    var3
    1   10      57       16
    2   8       66       34
    3   7       88       5
    4   1       90       94
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
AlexP
  • 147
  • 2
  • 9
  • Are the ids just row names ie row_numbers? You can use `rowid_to_column`(from tibble, exact function may differ), rownames or row.names or just mutate. – NelsonGon Aug 26 '19 at 18:01
  • 3
    `cbind(id=df2[,'ID'],df1)` – Onyambu Aug 26 '19 at 18:02
  • You don't even need to use dplyr, there are literally a million ways to achieve this. If you want it to strictly be the "first" column, just do df1<-cbind(id=df2[,"ID"],df1), or df1<-data.frame(ID=df2$ID,var1=df1$var1,var2=df1$var2,var3=df1$var3), etc. – Ghost Aug 26 '19 at 18:27
  • Perfect, thank you!! – AlexP Aug 26 '19 at 18:36

0 Answers0