Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just to address the last part of your question, since that really points out the difference between a <code>list</code> and <code>vector</code> in R:</p> <blockquote> <p>Why do these two expressions not return the same result?</p> <p>x = list(1, 2, 3, 4); x2 = list(1:4)</p> </blockquote> <p>A list can contain any other class as each element. So you can have a list where the first element is a character vector, the second is a data frame, etc. In this case, you have created two different lists. <code>x</code> has four vectors, each of length 1. <code>x2</code> has 1 vector of length 4:</p> <pre><code>&gt; length(x[[1]]) [1] 1 &gt; length(x2[[1]]) [1] 4 </code></pre> <p>So these are completely different lists. </p> <p>R lists are very much like <a href="http://en.wikipedia.org/wiki/Hash_table" rel="noreferrer">a hash map</a> data structure in that each index value can be associated with any object. Here's a simple example of a list that contains 3 different classes (including a function):</p> <pre><code>&gt; complicated.list &lt;- list("a"=1:4, "b"=1:3, "c"=matrix(1:4, nrow=2), "d"=search) &gt; lapply(complicated.list, class) $a [1] "integer" $b [1] "integer" $c [1] "matrix" $d [1] "function" </code></pre> <p>Given that the last element is the search function, I can call it like so:</p> <pre><code>&gt; complicated.list[["d"]]() [1] ".GlobalEnv" ... </code></pre> <p>As a final comment on this: it should be noted that a <code>data.frame</code> is really a list (from the <code>data.frame</code> documentation):</p> <blockquote> <p>A data frame is a list of variables of the same number of rows with unique row names, given class ‘"data.frame"’</p> </blockquote> <p>That's why columns in a <code>data.frame</code> can have different data types, while columns in a matrix cannot. As an example, here I try to create a matrix with numbers and characters:</p> <pre><code>&gt; a &lt;- 1:4 &gt; class(a) [1] "integer" &gt; b &lt;- c("a","b","c","d") &gt; d &lt;- cbind(a, b) &gt; d a b [1,] "1" "a" [2,] "2" "b" [3,] "3" "c" [4,] "4" "d" &gt; class(d[,1]) [1] "character" </code></pre> <p>Note how I cannot change the data type in the first column to numeric because the second column has characters:</p> <pre><code>&gt; d[,1] &lt;- as.numeric(d[,1]) &gt; class(d[,1]) [1] "character" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload