Search code examples
rlistloopssublist

Write a loop to pick up and merge overlapped part in a list


I have a large list (list_1920) shown below.

city01 <- data.frame("city" = "01", "gender" = c("female", "male"), "number" = c(12,20))
city02 <- data.frame("city" = "02", "gender" = c("female", "male"), "number" = c(50,40))
list2019 <- list(city01, city02)
names(list2019) = c("city01_2019", "city02_2019")
city01 <- data.frame("city" = "01", "gender" = c("female", "male"), "number" = c(60,80))
city02 <- data.frame("city" = "02", "gender" = c("female", "male"), "number" = c(120,90))
list2020 <- list(city01, city02)
names(list2020) = c("city01_2020", "city02_2020")
list_1920 <- list(list2019, list2020)
list_1920

Thanks to the help from community member, I can now use lapply to pick up info for the same city in different years:

city01 <- lapply(list_1920, function(x) x$city01)
city02 <- lapply(list_1920, function(x) x$city02)

city01
#> [[1]]
#>   city gender number
#> 1   01 female     12
#> 2   01   male     20
#> 
#> [[2]]
#>   city gender number
#> 1   01 female     60
#> 2   01   male     80

city02
#> [[1]]
#>   city gender number
#> 1   02 female     50
#> 2   02   male     40
#> 
#> [[2]]
#>   city gender number
#> 1   02 female    120
#> 2   02   male     90

I am now trying to combine the two steps and achieve the same result in a loop, since I need to do one more step analysis based on the result above. I have tried:

picklist <- tibble("city" = c("01", "02")
plist <- unique(picklist$city) # plist returns "01" and "02"
for (i in levels(plist)) {
  lst[[i]] <- lapply(list_1920, function(x) x$[[i]] 
# I hope this will return the same result shown above
  new <- merge(lst[[i]][[1]], lst[[i]][[2]])}
# lst[[i]][[1]] is what returned in city01 in the result shown above
# lst[[i]][[2]] is what returned in city02 in the result shown above

Unfortunately, this doesn't work. I received a message to debug lst[[i]] <- lapply(list_1920, function(x) x$[[i]]

It would be much appreciated if you could offer some help. Many thanks.


Solution

  • You can use lapply to pick out constant elements in the lists:

    city01 <- lapply(list_1920, function(x) x$city01)
    city02 <- lapply(list_1920, function(x) x$city02)
    
    city01
    #> [[1]]
    #>   city gender number
    #> 1   01 female     12
    #> 2   01   male     20
    #> 
    #> [[2]]
    #>   city gender number
    #> 1   01 female     60
    #> 2   01   male     80
    
    city02
    #> [[1]]
    #>   city gender number
    #> 1   02 female     50
    #> 2   02   male     40
    #> 
    #> [[2]]
    #>   city gender number
    #> 1   02 female    120
    #> 2   02   male     90
    

    Created on 2020-07-15 by the reprex package (v0.3.0)