-2

Suppose I have a list of tables. One of the columns of each of the tables is named and contains a count of NA values. For example, object 'e' in the below example shows a frequency of 2 in its last column:

a <- seq(1:5)
b <- c(NA,1,2,4,NA) 
c <- a %>% data.frame(.,b)
d <- table(c[1], useNA = "always")
e <- table(c[2], useNA = "always")
f <- list(d, e)

Is there a way to order the elements in the list based on the number of NAs in the NA column found in each table in the list? For example, in list f in the above, the NA column in element [1] (i.e. table d) indicates 0 NAs and the NA column in element [2] (i.e. table e) indicates 2 NAs. Since 2 > 0 an I would like to sort the elements of the list from most NAs to least NAs, the list would be reordered to list(e,d).

socialscientist
  • 3,759
  • 5
  • 23
  • 58
  • This question was very hard to understand. I've improved the spelling and formatting where possible. I also removed irrelevant comments. However, I don't understand the meaning of all of the dashes (`----`) or what you mean by "Right now I'm just using a dumb bubble sort which is easy to customize but it kind of hurts and I expect to find the same kind of issue further on." Can you please clarify your comments and edit your question to abide this: http://stackoverflow.com/help/mcve – Hack-R Jun 28 '16 at 02:03
  • The dashes were just there to mimick what one would see outputed to the console. – SolipsistElvis Jun 28 '16 at 02:04
  • OK, but not in R I think. That was part of why I was confused. I'll replace those with spaces for you if you'll read the linked resource. – Hack-R Jun 28 '16 at 02:05
  • I realize it is hard to understand because it is quite convoluted. What I mean if we forget this context is just "how to sort a list according to a comparison which I can implement, In this case that would according to elements in sublist. – SolipsistElvis Jun 28 '16 at 02:07
  • Also http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jun 28 '16 at 02:07
  • @Hack-R ok thanks for the edit. Why the downvote though? I feel like this has to be frequently encountered, and very counter intuitive in R for anybody coming from a C style language – SolipsistElvis Jun 28 '16 at 02:10
  • The question is terribly worded but it is an interesting problem. I have proposed a rewrite of the entire question. Essentially, SolipsistElvis is trying to order the elements of a list of tables by a comparison of each of the tables on the value of a commonly named column of each table. – socialscientist Jun 28 '16 at 02:31
  • The example could be simplified a lot more yet - e.g. - `a <- table(c(1:5,NA),useNA="always"); b <- table(c(1,2,4,NA,NA),useNA="always"); L <- list(a,b)` , which should be re-sorted as a result like `list(b,a)` – thelatemail Jun 28 '16 at 05:32

1 Answers1

2

Based on the description, we can loop over the elements of the list ('f'), subset the elements with NA as names, order it decreasingly and use that index to reorder the 'f'.

f1 <- f[order(-sapply(f, function(x) x[is.na(names(x))]))]
identical(f1, list(e,d))
#[1] TRUE
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, does this mean most of the things one would do in a C style language with overloading and classes definitions is done with functional style programming and, in this case, logical indexing, in R? – SolipsistElvis Jun 28 '16 at 18:11
  • @SolipsistElvis In `R`, many functions are vectorized, so the code writing part is easier. – akrun Jun 28 '16 at 18:14