1

I'd have to say that I've just started working with R half an hour ago, running R scripts from SQL Server, so I'm what they call a complete noob.

I'm trying to merge two R vectors into a data frame, but my problem is that the vectors have varying lengths.

I'm not sure if this can be done, although i don't necessarily see why not?

My R script is:

n <- c(2, 3, 5, 6);
s = c("aa", "bb", "cc");
df = data.frame(n, s);

And the error that I'm getting is:

Error in data.frame(n, s) : arguments imply differing number of rows: 4, 3 Calls: source -> withVisible -> eval -> eval -> data.frame

Error in ScaleR. Check the output for more information. Error in eval(expr, envir, enclos) : Error in ScaleR. Check the output for more information. Calls: source -> withVisible -> eval -> eval -> .Call Execution halted

Esentially what I'm imagining doing is generating a result set similar to a LEFT JOIN.

2    aa
3    bb
5    cc
6    NULL

I'm asking myself if I should specify a "replace NULL" values in the R data frame, but I'm not sure if this is a solution.

Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107
  • 2
    You can't have `NULL` there, but rather `NA` (for missing): `L = list(n = n, s = s); data.frame(lapply(L, \`length<-\`, max(lengths(L))))`. I guess there are a few dupes for this. – Frank May 18 '17 at 15:06
  • what you are refering to is not `merging`. To `merge` you need a key. It is not possible to combine `list` of different lengths into a data.frame in R. You have to make them the same length to combine them. How to you want to combine them? – DJJ May 18 '17 at 15:07
  • @DJJ I was looking online to see if a R dataframe has an implicit key generated for each row so that I can `JOIN` or map them, but I couldn't find anything (yet). – Radu Gheorghiu May 18 '17 at 15:08
  • You can build your own key and use the function `merge`. – DJJ May 18 '17 at 15:10
  • Thanks a lot for your help guys! This clears things up a bit – Radu Gheorghiu May 18 '17 at 15:10
  • Also http://stackoverflow.com/questions/43159384/convert-a-list-of-character-vectors-into-dataframe/43159405#43159405 – Frank May 18 '17 at 15:10
  • The provided dups are talking about `list` while this question talks about `vector`. I voted for reopen. – 989 May 18 '17 at 15:14
  • @Frank I'll be following your [R tutorial](http://franknarf1.github.io/r-tutorial/_book/index.html) and hopefully you won't need to close these kind of dup-questions. – Radu Gheorghiu May 18 '17 at 15:18
  • I was in the way of answering this when it was mark as a duplicate..... you can cbind your vector but then you will encounter the recycling rule. So you need to specify the lenght of your vector .... n <- max(length(d), length(e)) length(d) <- n length(e) <- n m <- cbind(d,e) – Nico Coallier May 18 '17 at 15:19
  • Why the downvote? I'm very curious. – Radu Gheorghiu May 18 '17 at 15:19
  • However , you need to understand that in R everything is a function and the data.frame function requires same lenght attributes. – Nico Coallier May 18 '17 at 15:20
  • http://stackoverflow.com/questions/3365885/combining-vectors-of-unequal-length-into-a-data-frame?noredirect=1&lq=1 – Nico Coallier May 18 '17 at 15:20
  • 1
    Cool, let me know how it goes. There's the R chat room if you have any comments or suggestions http://chat.stackoverflow.com/rooms/25312/r-public The downvote is probably coming from people who think you should have found the dupe on your own (not me in this case). (Btw, I added Nico's link to the dupe target list.) – Frank May 18 '17 at 15:23
  • 1
    @Frank Thanks for the additional info! I guess dupes were to be likely in this basic-type scenario, but with a limited context and knowledge of R like mine it seems a bit *"unfair"*. Although I did take the time to look at the Stackoverflow suggested questions to see if the problems were similar (but they weren't). – Radu Gheorghiu May 18 '17 at 15:26
  • 1
    Thank you @NicoCoallier! I'll be sure to follow all your links! I'm in the state of trying to find out about R and understand as much as possible, so every comment is a mini-gold-mine for me. – Radu Gheorghiu May 18 '17 at 15:31
  • 1
    @RaduGheorghiu I am currently working on a R formation ...It is currently only in french but have a look at the script you'll get a good understanding of the data type and format.... https://github.com/NicoCoallier/Formation_R/blob/master/Operation_de_base.R I'll make a English one soon :) – Nico Coallier May 18 '17 at 15:33

1 Answers1

6

This one maybe:

sq <- seq(max(length(n), length(s)))
data.frame(n[sq], s[sq])

#  n.sq. s.sq.
#1     2    aa
#2     3    bb
#3     5    cc
#4     6  <NA>
989
  • 12,579
  • 5
  • 31
  • 53