0

Code:

i<-c("a","b","c")
j<-c("x","y")
k<-data.frame(i,j)
Error in data.frame(i, j) :
arguments imply differing number of rows: 3, 2

I'm trying to take two vectors named i and j:

    i             j
1   a         1   x
2   b         2   y
3   c

and I would like to join them as a data frame such that the empty row in j is filled in and it looks like this:

    i    j
1   a    x
2   b    y
3   c    0

How do I achieve this and end up with class(k) being a data frame? Other examples are using:

cbind.fill<-function(...){
  nm <- list(...) 
  nm<-lapply(nm, as.matrix)
  n <- max(sapply(nm, nrow)) 
  do.call(cbind, lapply(nm, function (x) 
    rbind(x, data.frame(, n-nrow(x), ncol(x))))) 
}

But I need a data frame as the result.

user4014
  • 3
  • 1
  • 4
  • That's not what merge is for. – Glen_b Jun 21 '13 at 00:41
  • Please post a reproducible example, so (for example), we don't have to guess whether j is a factor or a character variable. – Glen_b Jun 21 '13 at 01:12
  • @Metrics - well spotted. I'll delete my answer. – Glen_b Jun 21 '13 at 01:26
  • I think the duplication in [combining two data frames of different lengths](http://stackoverflow.com/questions/6988184/combining-two-data-frames-of-different-lengths) is perhaps closer. – Glen_b Jun 21 '13 at 03:47

0 Answers0