0

Let me clear, I do not want to add, multiply, subtract or divide the data. I want the new column to include all the information from both the first column and the second column. Here is an example of what I mean.

     data 1      data 2    new data
1      Q            1          Q
2      T            5          T
3      R            3          R 
4                              1
5                              5
6                              3 

The values I am looking at are not categorical but I used them as an example to show the difference.

Aurthur M.
  • 13
  • 3
  • `union` is what you might be looking for. – A Gore Jul 17 '17 at 17:24
  • 1
    If the two data columns are contained in a two column data.frame, named "dat", you could do `Reduce(union, dat)`. – lmo Jul 17 '17 at 17:29
  • Create an on after insert trigger on data 1 that performs an update on on new data with the same value. Create an after insert trigger on data 2 that performs an insert with only new data set to the new value. – Ylli Prifti Jul 17 '17 at 17:35

1 Answers1

1
cbind.fill <- function(...){
  # From a SO answer by Tyler Rinker
  nm <- list(...)
  nm <- lapply(nm, as.matrix)
  n <- max(sapply(nm, nrow))
  do.call(cbind, lapply(nm, function (x)
    rbind(x, matrix(, n-nrow(x), ncol(x)))))
}

df1 <- data.frame("data 1"=c("Q","T","R"),"data 2"=c(1,5,3), stringsAsFactors = F)

df2 <- cbind.fill(df1, c(df1$data.1, df1$data.2))
colnames(df2) <- c(colnames(df2)[1:2], "new data")
df2
    data.1 data.2  new data  
[1,] "Q"    "1"    "Q"
[2,] "T"    "5"    "T"
[3,] "R"    "3"    "R"
[4,] NA     NA     "1"
[5,] NA     NA     "5"
[6,] NA     NA     "3"

Source of cbind.fill function: cbind a df with an empty df (cbind.fill?)

Hack-R
  • 22,422
  • 14
  • 75
  • 131