fabarin
fabarin

Reputation: 69

Select certain columns from A table to JOIN certain columns in B table in R

1.Simply like using Ctrl+X select a columns add Ctrl+V into a certain columns in another table freely.

2.Select bigger table's columns to join smaller table that full_join and by function cannot do.(also they have different cols names.)

#A table (bigger table)

 Manufactor Models  date  Serial
1       audi    r55 21341  34j
2        bmw    e44 13214  F34
3    cadillc   fr4c 23124  00deaa
4       benz   c45z 21415  3rf
5      lexus  l56fs 97014  3r
6     toyota   de22 75199  2ghre

#B table (smaller table)
      Markers   Price   Types   
1      Asaudi    4011    ar55   
2        abmw    2334    ae44
3    acadillc    1445   fsr4c
4       fbenz    1455  cdf45z
5     falexus  5551l5   ff6fs
6    12toyota   51242    de22

Expected picture

#B table
      Markers   Price   Types   
1      Asaudi    4011    ar55   
2        abmw    2334    ae44
3    acadillc    1445   fsr4c
4       fbenz    1455  cdf45z
5     falexus  5551l5   ff6fs
6    12toyota   51242    de22
7       audi      NA      r55
8        bmw      NA      e44
9    cadillc      NA     fr4c
10       benz     NA     c45z
11     lexus      NA    l56fs
12    toyota      NA     de22

Eliminate unnecessary cols in A table firstly, to fit full_join by =c("x col name"="y col name") limitation is the way but it is inefficient .Are there more clean and efficient way to do that?

Upvotes: 1

Views: 58

Answers (1)

Prem
Prem

Reputation: 11955

Your illustration suggests that you can achieve the expected result using below code snippet

library(dplyr)
A %>%
  select(-date, -Serial) %>%
  `colnames<-`(c('Markers','Types')) %>%
  bind_rows(B,.)

Output is:

    Markers  Price  Types
1    Asaudi   4011   ar55
2      abmw   2334   ae44
3  acadillc   1445  fsr4c
4     fbenz   1455 cdf45z
5   falexus 5551l5  ff6fs
6  12toyota  51242   de22
7      audi   <NA>    r55
8       bmw   <NA>    e44
9   cadillc   <NA>   fr4c
10     benz   <NA>   c45z
11    lexus   <NA>  l56fs
12   toyota   <NA>   de22

Sample data:

> dput(A)
structure(list(Manufactor = structure(c(1L, 3L, 4L, 2L, 5L, 6L
), .Label = c("audi", "benz", "bmw", "cadillc", "lexus", "toyota"
), class = "factor"), Models = structure(c(6L, 3L, 4L, 1L, 5L, 
2L), .Label = c("c45z", "de22", "e44", "fr4c", "l56fs", "r55"
), class = "factor"), date = c(21341L, 13214L, 23124L, 21415L, 
97014L, 75199L), Serial = structure(c(3L, 6L, 1L, 5L, 4L, 2L), .Label = c("00deaa", 
"2ghre", "34j", "3r", "3rf", "F34"), class = "factor")), .Names = c("Manufactor", 
"Models", "date", "Serial"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

> dput(B)
structure(list(Markers = structure(c(4L, 2L, 3L, 6L, 5L, 1L), .Label = c("12toyota", 
"abmw", "acadillc", "Asaudi", "falexus", "fbenz"), class = "factor"), 
    Price = structure(c(4L, 3L, 1L, 2L, 6L, 5L), .Label = c("1445", 
    "1455", "2334", "4011", "51242", "5551l5"), class = "factor"), 
    Types = structure(c(2L, 1L, 6L, 3L, 5L, 4L), .Label = c("ae44", 
    "ar55", "cdf45z", "de22", "ff6fs", "fsr4c"), class = "factor")), .Names = c("Markers", 
"Price", "Types"), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6"))

Upvotes: 1

Related Questions