从列表中删除具有特定字段的元素(r语言)

时间:2016-03-28 10:00:55

标签: r

我有一个列表矢量。每个列表都包含字段category 我需要删除包含字段category == resource的所有列表。

我试过

for(i in 1:length(myList)){
  if(myList[[as.numeric(i)]]$data$category == "resource"){
    myList[[as.numeric(i)]]<-NULL
  }
}

这是对的吗?

> dput(myList[1:2])
list(structure(list(data = structure(list(device = "iPad2,4", 
    os_version = "ios 7.1.2", connection_type = "wifi", category = "user", 
    platform = "ios", session_num = 2, ios_idfv = "E12AA218-4061-4BA1-AFA3-47FEE1511C2E", 
    client_ts = 1454926346, sdk_version = "unity 2.4.3", limited_ad_tracking = TRUE, 
    user_id = "436E8588-B2FA-411A-896C-5757E7A2A377", manufacturer = "apple", 
    jailbroken = TRUE, ios_idfa = "436E8588-B2FA-411A-896C-5757E7A2A377", 
    build = "1.0", session_id = "558910f2-7c2c-4280-84fc-2fef1a50d291", 
    v = 2, engine_version = "unity 4.6.9"), .Names = c("device", 
"os_version", "connection_type", "category", "platform", "session_num", 
"ios_idfv", "client_ts", "sdk_version", "limited_ad_tracking", 
"user_id", "manufacturer", "jailbroken", "ios_idfa", "build", 
"session_id", "v", "engine_version")), first_in_batch = TRUE, 
    country_code = "RO", arrival_ts = 1454926344, game_id = 24540, 
    ip = "93.168.249.0"), .Names = c("data", "first_in_batch", 
"country_code", "arrival_ts", "game_id", "ip")), structure(list(
    data = structure(list(os_version = "ios 9.2.1", engine_version = "unity 4.6.9", 
        category = "user", v = 2, ios_idfa = "F06962FE-FCE5-475F-8CC2-83FF1F89E573", 
        sdk_version = "unity 2.4.3", user_id = "F06962FE-FCE5-475F-8CC2-83FF1F89E573", 
        session_num = 2, platform = "ios", connection_type = "wifi", 
        manufacturer = "apple", client_ts = 1454925528, limited_ad_tracking = TRUE, 
        session_id = "3ab1fbbb-5103-4ad8-bf56-94f70fea94a7", 
        device = "iPad4,1", ios_idfv = "2C940E82-B074-4A15-B9A7-A5983759042F", 
        build = "1.0"), .Names = c("os_version", "engine_version", 
    "category", "v", "ios_idfa", "sdk_version", "user_id", "session_num", 
    "platform", "connection_type", "manufacturer", "client_ts", 
    "limited_ad_tracking", "session_id", "device", "ios_idfv", 
    "build")), first_in_batch = TRUE, country_code = "AU", arrival_ts = 1454925528, 
    game_id = 24540, ip = "110.175.52.0"), .Names = c("data", 
"first_in_batch", "country_code", "arrival_ts", "game_id", "ip"
)))

2 个答案:

答案 0 :(得分:1)

您可以使用rlist包:

library(rlist)
filtered_list <- list.exclude(myList, data$category == "ressource")

答案 1 :(得分:0)

我们可以使用Filter

中的base R
Filter(function(x) x$data$category!="resource", myList)