Looking back at my OP, I don't really see where the confusion is, I guess you guys deal in code and not words, I'll have to remember that.
I thought it was an easy enough question that someone could bang out an answer in a couple minutes. These are literally the first for() loops I've ever made, I have generally dealt with plotting and bioconductor packages, but once I figured out the for() loops and the functions I needed (like append() and rbind() it really was not that hard. [Look at my OP, it is CLEARLY an expanded for loop. Maybe people did not want to help for the simplicity, which is fine because it was better for me to figure it out myself.] Anyways, for reference sake, suggestions, appendices or derivations, this is my code. set.seed(69) vector = rnorm(100, sd=10) matrix = matrix(vector, ncol=10) #Loop finding difference of a vector from 1 value in it stepwise x=vector() g=vector() h=vector() for(l in 1:nrow(matrix)){ x=matrix[l,] for(i in 1:ncol(matrix)){ #h makes the differences, gives to g, rinse and repeat h=abs(x-x[i]); h[h==0]<-NA; g=append(g, h) } } #Turns output into a matrix that has a row for every difference set. g=matrix(g, ncol=length(x), byrow=TRUE) #Determines the least difference between 2 separate numbers in each row of the original matrix leastdifference=vector() loopleast=vector() for(p in 1:nrow(matrix)){ #Goes through the g matrix from 1:10, 11:20, etc.; loopleast finds the minimum difference, hands off the leastdifference and repeats loopleast <- which(g[((p*ncol(g))-9):(p*ncol(g)),] == min(g[(p*ncol(g)-9):(p*ncol(g)),], na.rm=TRUE), arr.ind = TRUE); leastdifference=rbind(leastdifference, loopleast[1,]) } colnames(leastdifference) <- c("","") leastdifference [1,] 9 2 [2,] 2 1 [3,] 8 1 [4,] 8 4 [5,] 9 3 [6,] 10 7 [7,] 4 1 [8,] 8 1 [9,] 9 7 [10,] 8 3 So, in the first row, 9 and 2 are least different, 2 and 1 in the second, 8 and 1 in the third, etc. I'm going to try StackOverflow for my next questions. On Wed, Sep 11, 2013 at 8:48 PM, Jeff Newmiller <jdnew...@dcn.davis.ca.us>wrote: > It is worth asking for clarification sometimes, but I have to admit that I > don't have much sympathy in this case because there isn't much code > involved and typing in the code (or copy/pasting it line-by-line) and > experimenting with it is crucial to the process of learning R. Picking out > one expression at a time from each line and looking at the result with the > str function is really how you have to do it. That is why responders on > this list so often ask for reproducible examples from the questioner... the > answer can be much more compact and quick to generate. > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnew...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live > Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > Ben Harrison <h...@student.unimelb.edu.au> wrote: > >If I were Michael (OP) right now, I think my head would be spinning. > > > >As a newbie myself, I know how hard it is to read R code for the first > >time, so could it also be part of the newsgroup etiquette to at least > >partially explain provided code to newbies? > > > >I agree that the interactive help '?' is available and should be > >consulted, but it would also be helpful if those of you with great > >experience could add a little guidance for your code. > > > >Excuse me if this is out-of-place. > > > >Ben > > > > > >On 11/09/13 09:48, Ben Bolker wrote: > >> On 13-09-10 06:24 PM, arun wrote: > >>> Hi, > >>> May be this also works: > >>> > >>> dist(x) > >>> # 1 2 3 > >>> #2 2 > >>> #3 6 4 > >>> #4 12 10 6 > >>> > >>> as.matrix(dist(x)) > >>> # 1 2 3 4 > >>> #1 0 2 6 12 > >>> #2 2 0 4 10 > >>> #3 6 4 0 6 > >>> #4 12 10 6 0 > >>> which(dist(x)==min(dist(x))) > >>> #[1] 1 > >>> A.K. > >> > >> Yes, but you need to set the diagonal to NA, or something -- the > >OP > >> doesn't want to include self-comparison. It also helps to use > >> arr.ind=TRUE in which(). You're right that dist() would be a hair > >more > >> efficient that outer(...), though > >> > >>> > >>> > >>> > >>> ----- Original Message ----- > >>> From: Ben Bolker <bbol...@gmail.com> > >>> To: r-h...@stat.math.ethz.ch > >>> Cc: > >>> Sent: Tuesday, September 10, 2013 5:39 PM > >>> Subject: Re: [R] Subtracting elements of a vector from each other > >stepwise > >>> > >>> arun <smartpink111 <at> yahoo.com> writes: > >>> > >>>> > >>>> Hi, > >>>> Not sure this is what you wanted: > >>>> > >>>> sapply(seq_along(x), function(i) {x1<- x[i]; x2<- x[-i]; > >>> x3<-x2[which.min(abs(x1-x2))];c(x1,x3)}) > >>>> # [,1] [,2] [,3] [,4] > >>>> #[1,] 17 19 23 29 > >>>> #[2,] 19 17 19 23 > >>>> A.K. > >>> > >>> > >>> It's a little inefficient (because it constructs > >>> the distances in both directions), but how about: > >>> > >>> x = c(17,19,23,29) > >>> d <- abs(outer(x,x,"-")) > >>> diag(d) <- NA > >>> d[lower.tri(d)] <- NA > >>> which(d==min(d,na.rm=TRUE),arr.ind=TRUE) > >>> > >>> > >>> ? > >>> > >>>> ----- Original Message ----- > >>>> From: Michael Budnick <mbudnick08 <at> snet.net> > >>>> To: r-help <at> r-project.org > >>>> Cc: > >>>> Sent: Tuesday, September 10, 2013 4:06 PM > >>>> Subject: [R] Subtracting elements of a vector from each other > >stepwise > >>>> > >>>> I am trying to figure out how to create a loop that will take the > >>>> difference of each member of a vector from each other and also spit > >out > >>>> which one has the least difference. > >>>> > >>>> I do not want the vector member to subtract from itself or it must > >be able > >>>> to disregard the 0 obtained from subtracting from itself. > >>>> > >>>> For example: > >>>> > >>>> x = c(17,19,23,29) > >>> > >>> > >>> [snip] > >>> > >>> ______________________________________________ > >>> 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. > > [[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.