1

Been trying to resolve the following for a couple of hours now.

So I have a list, say:

Wyoming <- c("City1", "City2", "City3", ...)
Ontario <- c("City1", "City2", "City3", ...)
Florida <- c("City1", "City2", "City3", ...)
... # for all federated states

Mexico <- list(Sonora, Yucatan, Chiapas, Sinaloa)
Canada <- list(Alberta, Quebec, Ontario, Manitoba)
USA <- list(Wyoming, Colorado, NewHampshire, Florida)


ListOriginal <- list(USA, Canada, Mexico)

I'd like to remove the parent level (USA, Canada, Mexico) but keep the federated states, all of them in a single list. So it would appear like this:

$Wyoming
[1] "City1" "City2" "City3" ... 

$Colorado
[1] "City1" "City2" "City3" ... 

$Chiapas
[1] "City1" "City2" "City3" ... 

#etc... for every federated state

With everything I have been trying so far, for instance when I use the following...

NorthAmericaFedStates <- sapply(ListOriginal, "[[", 1) 

... the problem is that it will keep the top "parent", in this case the country name, and will show the cities directly, probably like this:

$Mexico
[1] "City1" "City2" "City3"
[2] "City1" "City2" "City3"
[3] "City1" "City2" "City3"
# etc

$Canada
[1] "City1" "City2" "City3"
[2] "City1" "City2" "City3"
[3] "City1" "City2" "City3"
# etc

I am able to choose what I want and show it in the console and save the result in a variable, but when I print the variable it will have the same problem as above.

ExactDataIWant <- function(x) {
  Data <- x[1]
  print(blabla)
}

ExactData <- lapply(OriginalList, ExactDataIWant)

This above shows me the data I want, (the state, then the cities), but when I type ExactData to print it in the console it gets me back to the start, with the country on top.

Any help please?

  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Using an incomplete example ("etc","...") makes it very difficult to test. – MrFlick Aug 02 '18 at 14:57
  • Using an ellipsis in example construction is a prescription for a non-reproducible example. It suggests an expectation that we will be able to read your mind and be willing to complete a task that you thought was obvious but not worth your time to complete. – IRTFM Aug 02 '18 at 15:27
  • Ok, sorry! Still not used to all this – EvetsLefurac Aug 02 '18 at 18:48

2 Answers2

1

It sounds like maybe you just want unlist

unlist(unname(ListOriginal), recursive = FALSE)

I added the unname part to get rid of the country names, otherwise they are preserved by appending them to the region names.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
0

Thanks guys!

It's... amazingly simple... was not aware of the rlist package sufficiently I guess!

It seems that this really did the trick:

library(rlist)

list.ungroup(ListOriginal, level = 1L, group.names = FALSE, sort.names = FALSE)

What's great is that it does not seem to matter whether one list item will have one, two, three or four sub-items. All your second-level items within your list will all be at the same level, not discriminating whether some of those items were sharing a parent or not. I think you can put group.names to TRUE if one wants to keep track of this.