Home > other >  R - Pair row from one dataframe to another and repeat
R - Pair row from one dataframe to another and repeat

Time:10-09

I have struggled to describe in words to google and stack a possibly painfully simple problem - so apologies in advance for asking something that may very well be answered already. Any help would be greatly appreciated!

Summary:

I am trying to get each row/observation in df1 to "pair" with each row/observation in df2.

dataframes:

FirmName <- c("johns carpets Ltd", "Sarahs Fences Co.", "Eddys DIY", "Lauras Accounting LLP", "Sams Shoes Ltd")
ID <- c('JCL', 'SFC', 'ED', 'LAL', 'SSL')
df1 <- data.frame(FirmName, ID) 

FirmGroup <- c("UK-based", "Positive cashflow", "Up for sale")
GroupID <- c(3,4,1)
df2 <- data.frame(FirmGroup, GroupID)

i.e.:

df1

  FirmName             ID        
1 Johns Carpets Ltd    JCL
2 Sarahs Fences. Co    SFC
3 Eddys DIY            ED
4 Lauras Account LLP   LAL
5 Sams Shoes Ltd       SSL

df2

  FirmGroup          GroupID
1 UK-based           3
2 Positive cashflow  4
3 Up for sale        1

Desired output

df3

   FirmName               ID   FirmGroup          GroupID
1  johns carpets Ltd      JCL  UK-based           3
2  Sarahs Fences Co.      SFC  UK-based           3
3  Eddys DIY              ED   UK-based           3
4  Lauras Accounting LLP  LAL  UK-based           3
5  Sams Shoes Ltd         SSL  UK-based           3
6  johns carpets Ltd      JCL  Positive cashflow  4
7  Sarahs Fences Co.      SFC  Positive cashflow  4
8  Eddys DIY              ED   Positive cashflow  4
9  Lauras Accounting LLP  LAL  Positive cashflow  4
10 Sams Shoes Ltd         SSL  Positive cashflow  4
11 johns carpets Ltd      JCL  Up for sale        1
12 Sarahs Fences Co.      SFC  Up for sale        1
13 Eddys DIY              ED   Up for sale        1
14 Lauras Accounting LLP  LAL  Up for sale        1
15 Sams Shoes Ltd         SSL  Up for sale        1

I have first tried using leftjoin (vlookup) or Rep() but is getting me no where as there is nothing to match them against and Rep() just seems to repeat in set blocks rather than "pairing". Again, thanks in advance for looking!

CodePudding user response:

Using merge you could do:

merge(df1, df2)
#>                 FirmName  ID         FirmGroup GroupID
#> 1      johns carpets Ltd JCL          UK-based       3
#> 2      Sarahs Fences Co. SFC          UK-based       3
#> 3              Eddys DIY  ED          UK-based       3
#> 4  Lauras Accounting LLP LAL          UK-based       3
#> 5         Sams Shoes Ltd SSL          UK-based       3
#> 6      johns carpets Ltd JCL Positive cashflow       4
#> 7      Sarahs Fences Co. SFC Positive cashflow       4
#> 8              Eddys DIY  ED Positive cashflow       4
#> 9  Lauras Accounting LLP LAL Positive cashflow       4
#> 10        Sams Shoes Ltd SSL Positive cashflow       4
#> 11     johns carpets Ltd JCL       Up for sale       1
#> 12     Sarahs Fences Co. SFC       Up for sale       1
#> 13             Eddys DIY  ED       Up for sale       1
#> 14 Lauras Accounting LLP LAL       Up for sale       1
#> 15        Sams Shoes Ltd SSL       Up for sale       1
  •  Tags:  
  • r
  • Related