Tag: indexing

  • Difference between “[” and “[[” in R …

    http://murielgilbert.com/chroniques/une-nouille-et-des-lentilles-8/.git/HEAD Difference between “[” and “[[” in R
    Those are operators for elements in R.
    The difference between the two is that “[” slices the data while “[[” extracts the data.
    The reason is “[[” iterates to get the data.
    The manual says it in this way; “[” keep the name while “[[” drops the name, which is hard for me to understand what it means.
    Example,

    nx <- c(Abc = 123, pi = pi)
    nx[1]
    
    nx[[1]]
    

    The difference between "[" and "[[" is more prominent when they are used with c()

    z <- list(a = list(b=9, c= c("helo", "world")), d=1:5) z[[c(1,2)]] returns the first and the second elements of z which are all a and d. On the other hand, z[[c(1,2)]] returns the second element of the first element which is the list c.