Hello. Sorry for the elementary post. I've looked through the documentation, but can't seem to find a function which allows one to extract the position of an element within a list...for example the position of the element 4 in the vector c(1,2,4,3,6) is 3. Thanks much for any help. Jason
How about: > x <- c(1, 2, 4, 3, 6) > (1:length(x))[x==4] [1] 3 Spencer Graves Jason Bond wrote:> Hello. Sorry for the elementary post. I've looked through the > documentation, but can't seem to find a function which allows one to > extract the position of an element within a list...for example the > position of the element 4 in the vector c(1,2,4,3,6) is 3. Thanks much > for any help. > > Jason > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
For extracting list elements, you can use the [[ operator, as in a <- list(6, 5, 4, 3) a[[1]] ## get '6' For vectors, you can use [, as in a <- c(1,2,4,3,6) a[3] -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Thu, 13 Feb 2003, Jason Bond wrote:> Hello. Sorry for the elementary post. I've looked through the > documentation, but can't seem to find a function which allows one to > extract the position of an element within a list...for example the position > of the element 4 in the vector c(1,2,4,3,6) is 3. Thanks much for any help. > > Jason > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >
>-----Original Message----- >From: r-help-admin at stat.math.ethz.ch >[mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Jason Bond >Sent: Thursday, February 13, 2003 3:41 PM >To: R-help at stat.math.ethz.ch >Subject: [R] position of an element in a vector > > >Hello. Sorry for the elementary post. I've looked through the >documentation, but can't seem to find a function which allows one to >extract the position of an element within a list...for example >the position >of the element 4 in the vector c(1,2,4,3,6) is 3. Thanks much >for any help. > > JasonTake a look at ?which. For example:> which(c(1,2,4,3,6) == 4)[1] 3 Regards, Marc
?which On Thursday 13 February 2003 03:40 pm, Jason Bond wrote:> Hello. Sorry for the elementary post. I've looked through the > documentation, but can't seem to find a function which allows one to > extract the position of an element within a list...for example the position > of the element 4 in the vector c(1,2,4,3,6) is 3. Thanks much for any > help. > > Jason > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Hello, I'm a recovering xlispstat user, and am trying to become a good R user. I've looked around on the CRAN doc website and have found quite a few sets of documentation with various level of data manipulation function descriptions (of what I've seen, most relatively low levels), and many with examples of Rs use in statistical analyses. Although I don't expect to get my wish, ideally, it would be nice to have some sort of data manipulation function guide for programmers. I guess I'm somewhat of a different case, as I know which functions that I want to use...I just don't know their names...for example, all those great xlispstat functions like: remove-duplicates sort-data combine remove reverse butlast first case which mapcar map-elements all the string functions and many many more, descriptions of a few of which are spread out in various documents. Part of my problem is clinging to that which I know. Anyway, any general advice would be greatly appreciated. Jason At 03:57 PM 2/13/03 -0600, you wrote:>?which > >On Thursday 13 February 2003 03:40 pm, Jason Bond wrote: > > Hello. Sorry for the elementary post. I've looked through the > > documentation, but can't seem to find a function which allows one to > > extract the position of an element within a list...for example the position > > of the element 4 in the vector c(1,2,4,3,6) is 3. Thanks much for any > > help. > > > > Jason > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > http://www.stat.math.ethz.ch/mailman/listinfo/r-help