【问题标题】:Working with list in R在 R 中使用列表
【发布时间】:2014-03-13 11:14:02
【问题描述】:

我在 R 中有很多这样的列表:

[[1]]
[[1]]$long_name
[1] "Ħal Għaxaq"

[[1]]$short_name
[1] "Ħal Għaxaq"

[[1]]$types
[1] "locality"  "political"


[[2]]
[[2]]$long_name
[1] "Malta"

[[2]]$short_name
[1] "MT"

[[2]]$types
[1] "country"   "political"

有时列表有超过 2 个元素。对于每个列表,我只需要“读取”列表中 $types "country" 中元素的 $long_name

【问题讨论】:

    标签: json r list


    【解决方案1】:
    data <- list(list(long_name = "Hal Ghaxaq", 
                      short_name = "Hal Ghaxaq", 
                      types = c("locality", "political")), 
                 list(long_name = "Malta", 
                      short_name = "MT", 
                      types = c("country", "political")))
    result <- data[ sapply(data, function(el) "country" %in% el$types)]
    

    【讨论】:

    • 非常感谢 mutch,我发现在 R 中使用 listS 非常辛苦,你救了我的命
    猜你喜欢
    • 2017-01-09
    • 1970-01-01
    • 2020-05-27
    • 2020-06-09
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多