0

I have a question that is related to this post. I don't understand what this line does:

    col2=unlist(lapply(adjlist,"[",-1))

I mean, I know it's turning the all columns but the first one into one long column that is then merged with col1 but I don't get how and I don't want to use a code that I don't understand.

Thank you!

Community
  • 1
  • 1
Justyna
  • 737
  • 2
  • 10
  • 25
  • 1
    I'd suggest you create and look at the intermediate object `lapply(adjlist,"[",-1)` and review the help files `?lapply`, `?[` and `?unlist`. – Frank Nov 24 '15 at 19:16
  • I know what `lapply` and `unlist` do but I don't understand how the `"[", -1` turns rows into column. And yes, I checked help before posting my question. I can guess that `-1` is for skipping the first element in each row. – Justyna Nov 24 '15 at 19:29
  • Make a small reproducible example fully contained in this post and it's more likely that someone can help. (1) `?\`[.data.frame\`` explains that `DF[i]` selects columns; (2) a data.frame is a list of columns so `unlist` will do what `unlist` does, making them into a single vector. – Frank Nov 24 '15 at 19:33
  • I understand all of that -- what I don't understand is this particular part of the syntax of this command. I don't know how to explain it better. The code provided in the link works perfectly fine but I don't understand why or how. I know that `df[i]` will select a column number `i` but here I have only the left bracket. Does `lapply(adjlist, "[", -1)` takes each consecutive row of the `adjlist` and removes the first element and then `unlist` turns it into a long vector? – Justyna Nov 24 '15 at 20:01
  • The left bracket is the name of the function used, even in the double-bracket form. `"["(DF, 2:3)` and `\`[\`(DF,2:3)` and `DF[2:3]` all do the same thing. Similarly, `(` is a function, and `[[`. It's a massive read, but I'm sure this is covered in the main doc for the language https://cran.r-project.org/doc/manuals/r-release/R-lang.html – Frank Nov 24 '15 at 20:10
  • 1
    @Frank I think at this point it would be more pedagogical just to whip up a small example and explain what -1 is removing - in an answer. :) – Roman Luštrik Nov 24 '15 at 22:32
  • @RomanLuštrik Oops, I was thinking that was a good idea, but now I've gone and read the linked question and realized I have no idea what it's doing (since I've never needed `scan()`). Turns out they are indeed iterating over rows, and not a list of data.frames. Anyways, I guess we can close this as "why is my code not working?" or similar, unless someone wants to painstakingly make an example similar to the linked one and write the accompanying tutorial. – Frank Nov 24 '15 at 23:51

0 Answers0