Aren't you mixing up [[ and [ ? > x <- 1:3 > x[0] integer(0) > x[[0]] Error in x[[0]] : attempt to select less than one element > x[4] [1] NA > x[[4]] Error in x[[4]] : subscript out of bounds
The docs say: "The default methods work somewhat differently for atomic vectors, matrices/arrays and for recursive (list-like, see is.recursive) objects." -- which we certainly see in the above differences. So even though both will do the same thing in certain circumstances (e.g. x[1] = x[[1]] = 1), [[ should really be reserved for recursive (list-like) objects. Unfortunately, there are certain circumstances where you need to use [ on lists: for example, note that whether component names are preserved depends on how you do things: > y1 <- list(a=1:3) > y2 <- list(b=4,d=5) > c(y1,y2) $a [1] 1 2 3 $b [1] 4 $d [1] 5 > c(y1,y2[1]) $a [1] 1 2 3 $b [1] 4 > c(y1,y2[[1]]) $a [1] 1 2 3 [[2]] [1] 4 I think the detailed "explanation" of this behavior is that [[ selects a component, the atomic vector with single element 4, while [ selects a list containing this atomic vector as it's first component with name "b". c() then does it's thing: in the first 2 cases, it just concatenates the two lists; in the third, it first creates a list containing the single unnamed atomic vector and then concatenates this with y1. Clarification/correction of this would be appreciated if I haven't got it right or am missing something important. I've found V&R's S PROGRAMMING helpful in clarifying some of these semantic complexities (and sometimes inconsistencies?), but I would appreciate any suggestions for other and especially more complete explanations of all of this, as V&R doesn't cover everything (and is more S language focused rather than R-centric in detail). I think it's fair to say that the man pages do not provide all of the details, either. As a result, I still occasionally get bitten by these indexing subtleties (which, of course, may just be due to my dummheit). Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics 650-467-7374 -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stavros Macrakis Sent: Tuesday, April 07, 2009 9:11 AM To: Melissa2k9 Cc: r-help@r-project.org Subject: Re: [R] Sequences On Tue, Apr 7, 2009 at 8:13 AM, Melissa2k9 <m.mcquil...@lancaster.ac.uk>wrote: > > I am trying to make a sequence and am using a for loop for this. I want to > start off with an initial value ie S[0]=0 then use the loop to create other > values. This is what I have so far but I just keep getting error messages. > R only allows positive integer subscripts, and defines s[0] as the empty vector. You might think that assigning to an empty vector would give an error, but R semantics say instead that this is a no-op. This is so that things like s[ <<some boolean condition>> ] <- value will assign the value to all elements of s meeting the condition -- while allowing for the possibility that none of them meet that condition. If you try to assign to an illegal individual element, however, you do get an error: s[[0]] <- 45 # gives an error R's subscripting semantics are very convenient for many operations, but do take some getting used to. -s [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.