Hi, I have a list of vectors (of varying lengths). I'd like to sort this list by applying a function to each pair of vectors in the list and returning information to sorting routine that let's it know which one is larger. To solve problems like this in Common Lisp, the sort function accepts a function as an argument. The arguments to this function are two elements of the list which is being sorted. The writer of the function returns t (TRUE in R) when the first argument to the function is larger than the second and nil (FALSE in R) otherwise. I'm wondering if there is some way to accomplish this in R. Many thanks for any help! Cheers, David
On Feb 6, 2010, at 1:21 PM, David Neu wrote:> Hi, > > I have a list of vectors (of varying lengths). I'd like to sort this > list by applying a function to each pair of vectors in the list and > returning information to sorting routine that let's it know which one > is larger. > > To solve problems like this in Common Lisp, the sort function accepts > a function as an argument. The arguments to this function are two > elements of the list which is being sorted. The writer of the > function returns t (TRUE in R) when the first argument to the function > is larger than the second and nil (FALSE in R) otherwise. > > I'm wondering if there is some way to accomplish this in R. >Here's one way, although there may be options within the netherworld of S4 methods that I am not smart enough to navigate: GT <- function(x,y) x > y x <- c(8,7,4,2,5,7,5,8,4,5,8,3,0) > sum(GT(x[1],x)) [1] 10 # so the first element is greater than 10 other elements x[order(rowSums(sapply(x, GT, y=x)) )] # compare the number of other elements one by one and sort by the direction of your choice # [1] 8 8 8 7 7 5 5 5 4 4 3 2 0 #There's probably a method around the "reversal" of the usual sort order: > x[order(rowSums(sapply(x, GT, y=x)) ,decreasing=TRUE)] [1] 0 2 3 4 4 5 5 5 7 7 8 8 8 Perhaps use instead negation of the logical matrix that the sapply creates: > x[order(rowSums(!sapply(x, GT, y=x)) )] [1] 0 2 3 4 4 5 5 5 7 7 8 8 8 > sortFn <- function(x, FUN=">", ...) x[order(rowSums(!sapply(x, GT, y=x)) , ...)] > sortFn(x, GT) [1] 0 2 3 4 4 5 5 5 7 7 8 8 8 > sortFn(x, GT, decreasing=TRUE) [1] 8 8 8 7 7 5 5 5 4 4 3 2 0 -- David Winsemius, MD Heritage Laboratories West Hartford, CT
I have trouble making sense of the question, and I wonder if there is a terminology issue. For example, you have a list like this one: mylist <- list( v1=1:3, v2=1:4, v3=1:5, v4=1:6) (That is, a list of vectors of varying lengths.) You want to apply a function to each pair of vectors: First to v1 and v2, Then to v2 and v3, Then to v3 and v4 ? And also to pairs v1 and v3, v1 and v4, and so on? Which one is larger, mylist$v1 or mylist$v2? Longer, yes, mylist$v2 is longer. But larger? And ultimately you want to have the list with its elements in some other order, perhaps v4 comes first, then v3, and so on? -Don At 1:21 PM -0500 2/6/10, David Neu wrote:>Hi, > >I have a list of vectors (of varying lengths). I'd like to sort this >list by applying a function to each pair of vectors in the list and >returning information to sorting routine that let's it know which one >is larger. > >To solve problems like this in Common Lisp, the sort function accepts >a function as an argument. The arguments to this function are two >elements of the list which is being sorted. The writer of the >function returns t (TRUE in R) when the first argument to the function >is larger than the second and nil (FALSE in R) otherwise. > >I'm wondering if there is some way to accomplish this in R. > >Many thanks for any help! > >Cheers, >David > >______________________________________________ >R-help at 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
David Neu <david <at> davidneu.com> writes:> > Hi, > > I have a list of vectors (of varying lengths). I'd like to sort this > list by applying a function to each pair of vectors in the list and > returning information to sorting routine that let's it know which one > is larger. > > To solve problems like this in Common Lisp, the sort function accepts > a function as an argument. The arguments to this function are two > elements of the list which is being sorted. The writer of the > function returns t (TRUE in R) when the first argument to the function > is larger than the second and nil (FALSE in R) otherwise. > > I'm wondering if there is some way to accomplish this in R.Would the following function do what you want? sortList <- function(L, fun) L[order(sapply(L, fun))] Here is my test and my understanding of your request; L <- list() # define a list of vectors of varying length for (i in 1:10) { n <- sample(1:10, 1); L[[i]] <- runif(n) } Ls <- sortList(L, mean) sapply(Ls, mean) # increasing mean values Hans Werner> Many thanks for any help! > > Cheers, > David > >
Hi Don, Thanks for your response.> Which one is larger, mylist$v1 or mylist$v2? Longer, yes, mylist$v2 is > longer. But larger?Sure, given any two vectors the goal is to be able to use any criteria to determine if the first vector is "larger than" the second one. The key requirement is that depending on the criteria I may need both vectors in hand to perform the test. Cheers, David On Sat, Feb 6, 2010 at 3:00 PM, Don MacQueen <macq at llnl.gov> wrote:> I have trouble making sense of the question, and I wonder if there is a > terminology issue. > > For example, you have a list like this one: > > ?mylist <- list( v1=1:3, v2=1:4, v3=1:5, v4=1:6) > > (That is, a list of vectors of varying lengths.) > > You want to apply a function to each pair of vectors: > ?First to v1 and v2, > ?Then to v2 and v3, > ?Then to v3 and v4 > ? > And also to pairs v1 and v3, v1 and v4, and so on? > > Which one is larger, mylist$v1 or mylist$v2? Longer, yes, mylist$v2 is > longer. But larger? > > And ultimately you want to have the list with its elements in some other > order, > perhaps v4 comes first, then v3, and so on? > > -Don > > > At 1:21 PM -0500 2/6/10, David Neu wrote: >> >> Hi, >> >> I have a list of vectors (of varying lengths). ?I'd like to sort this >> list by applying a function to each pair of vectors in the list and >> returning information to sorting routine that let's it know which one >> is larger. >> >> To solve problems like this in Common Lisp, the sort function accepts >> a function as an argument. ?The arguments to this function are two >> elements of the list which is being sorted. ?The writer of the >> function returns t (TRUE in R) when the first argument to the function >> is larger than the second and nil (FALSE in R) otherwise. >> >> I'm wondering if there is some way to accomplish this in R. >> >> Many thanks for any help! >> >> Cheers, >> David >> >> ______________________________________________ >> R-help at 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. > > > -- > -------------------------------------- > Don MacQueen > Environmental Protection Department > Lawrence Livermore National Laboratory > Livermore, CA, USA > 925-423-1062 > -------------------------------------- >