使用R

时间:2018-02-24 15:48:12

标签: r list

您好我有这个列表,其中包括其他列表。

LIST <- list(list(c("USA","DC","MD","Washigton","Baltimore", 'Russia')), 
                list(c("USA","DC","MD","TX","Washigton","Baltimore","Dallas",'Russia',"France")), 
                list(c("USA","DC","MD","TX","Washigton","Baltimore","Dallas")),
                list(c("USA","DC","MD","TX","Washigton","Baltimore","Dallas",'Russia' )),
                list(c("USA","DC","Washigton")),
                list(c("USA","DC","Washigton","France")))  

我的目标是以这种形式显示:&#34; DC&#34;,&#34; Washigton&#34;,&#34; MD&#34;,&#34; Baltimore&#34;,& #34;俄罗斯&#34;这意味着城市应该遵循国家,如果有另一个国家/地区应该最后显示。在这一点上,我已经实现了这个目标:

output <- list()
for(i in 1:length(LIST)){
  country  <- sort( unique( LIST[[i]][[1]][ !sapply(LIST[[i]][[1]], function(y){foo(y)})  ] ))
  USAcheck <- any( country %in% 'USA')
  country  <- country[! country %in% 'USA']
  state    <- sort( unique( LIST[[i]][[1]][ sapply(LIST[[i]][[1]], function(y){foo(y)})  ] ))


  if(USAcheck){

    if( length(state) > 0){
      myString <- 'USA ('

      for(j in 1:length(state)){
        if(j == length(state)){
          myString <- paste0(myString, state[j], "), ")
        }else{
          myString <- paste0(myString, state[j], ", ")

        }
      }
    }else{
      myString <- 'USA, '
    }



  }else{
    myString <- ''
  }



  for(j in 1:length(country)){

    if(j == length(country)){
      myString <- paste0(myString, country[j])
    }else{
      myString <- paste0(myString, country[j], ", ")
    }

  }
  output[[i]] <- list( myString )

}

并取得了这个结果:

[[1]]
[[1]][[1]]
[1] "DC, MD Baltimore, Russia, Washigton"


[[2]]
[[2]][[1]]
[1] "DC, MD, TX Baltimore, Dallas, France, Russia, Washigton"


[[3]]
[[3]][[1]]
[1] "DC, MD, TX Baltimore, Dallas, Washigton"


[[4]]
[[4]][[1]]
[1] "DC, MD, TX Baltimore, Dallas, Russia, Washigton"


[[5]]
[[5]][[1]]
[1] "DC Washigton"


[[6]]
[[6]][[1]]
[1] "DC France, Washigton"

可能有帮助的提示是,在我的初始列表状态和城市匹配的方式,例如我有&#34; DC&#34;,&#34; MD&#34;,&#34; Washigton& #34;&#34;巴尔的摩&#34; &#34; DC&#34;与&#34;匹配华盛顿&#34;和&#34; MD&#34;与巴尔的摩&#34;。这就像第一个单词匹配第三个,第二个单词匹配第四个。在这里&#34; DC&#34;,&#34; MD&#34;,&#34; TX&#34;,&#34; Washigton&#34;,&#34; Baltimore&#34;,&#34;达拉斯&#34;这又是一个逻辑。如果没有通用的方法可以实现这一点,我希望能够改变这个顺序&#34;手动&#34;在每个子列表中。 有什么建议吗?

这就是我的原始列表的样子:

> output
[[1]]
[[1]][[1]]
[1] "Argentina" "Brazil"    "Colombia"  "Mexico"   


[[2]]
[[2]][[1]]
[1] "Australia" "India"    


[[3]]
[1] "DC, MD, Washington, Baltimore"

[[4]]
[1] "MA, Boston"

[[5]]
[1] "CO, Aurora"

[[6]]
[1] "MA, Boston"

[[7]]
[[7]][[1]]
[1] "Belgium"


[[8]]
[[8]][[1]]
[1] "Belgium" "Italy"   "UK"     


[[9]]
[1] "USA (AL, AZ, CA, CO, CT, DC, DE, FL, GA, IA, IL, IN, KY, LA, MA, MD, ME, MI, MN, MO, NC, ND, NE, NH, NJ, NM, NY, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VA, WI, WV), Australia, Canada, Israel, Korea, Mexico, Switzerland, Taiwan"

[[10]]
[[10]][[1]]
[1] NA


[[11]]
[1] "MA, Boston"

[[12]]
[1] "MA, WA, Boston, Seattle, France"

[[13]]
[[13]][[1]]
[1] "Canada"


[[14]]
[[14]][[1]]
[1] "China"


[[15]]
[1] "MA, TN, Boston, Nashville"

[[16]]
[[16]][[1]]
[1] "Denmark"


[[17]]
[1] "USA (NH, NJ, NM, NY) "

[[18]]
[1] "USA (CA, DC, IL, IN, MA, MD, MI, TN, TX) "

[[19]]
[1] "USA (AL, CA, NH, NJ, NM, NY, TN) "

[[20]]
[1] "USA (CA, IN, MA, NH, NJ, NM, NY, PA) "

[[21]]
[[21]][[1]]
[1] "Canada"


[[22]]
[[22]][[1]]
[1] NA


[[23]]
[1] "USA (CA, CT, MA, MO, NC, ND, NH, NJ, NM, NY, OH, TN, WA), Korea, Netherlands, Spain"

[[24]]
[[24]][[1]]
[1] "Sweden"


[[25]]
[1] "TX, Dallas"

0 个答案:

没有答案