-1

I would like to "copy paste" one column's value from df A under DF B's column values.

Below is I've visualized on what I'm trying to achieve

My Objective

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures aren't reproducible since we can't copy/paste the data inside. – MrFlick May 17 '19 at 15:18

2 Answers2

0

An option is to use bind_rows for the selected columns after making the type of the column same

library(dplyr)
bind_rows(df2, df1[1] %>% 
                 transmute(ColumnC = as.character(ColumnA)))
#  ColumnC ColumnD
#1       a       b
#2       1    <NA>
#3       2    <NA>
#4       3    <NA>

data

df1 <- data.frame(ColumnA = 1:3, ColumnB = 4:6)
df2 <- data.frame(ColumnC = 'a', ColumnD = 'b', 
         stringsAsFactors = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You may use also R base for this. You actually want to right join df2 with df1 :

df1 <- data.frame(1:3, 4:6)
names(df1) <- paste0("c", 1:2)
df2 <- data.frame("a", "b")
names(df2) <- paste0("c", 3:4)

# renaming column to join on
names(df2)[1] <- "c1"
merge(x = df1[,1,drop=FALSE], y = df2, by.y = c("c1"), all = TRUE)
cbo
  • 1,664
  • 1
  • 12
  • 27