删除包含零值的列表元素

时间:2018-03-04 21:26:08

标签: r

我有以下结构

'data.frame':   0 obs. of  18 variables:
 $ IDnumber            : chr 
 $ sctrs               : chr 
 $ Region              : chr 
 $ status              : num 
 $ EBIT.TA             : num 
 $ EBITDA.TA           : num 
 $ NI.SALES            : num 
 $ EQ.TA               : num 
 $ CL.TA               : num 
 $ logSALES            : num 
 $ logTA               : num 
 $ WC.TA               : num 
 $ SALES.TA            : num 
 $ LTD.TA              : num 
 $ WC.SALES            : num 
 $ EBIT.CAP            : num 
 $ Major.sectors.id    : num 
 $ Region.in.country.id: num 

这是我所拥有的列表中的一个元素,许多列表都填充了数据,但有些列表并不是由于缺少数据。

我一直试图找到一种方法来删除包含零观察值的列表元素。

填充的元素可能看起来像;

'data.frame':   2609 obs. of  18 variables:
 $ IDnumber       : chr  "ESB21183231" "ESB21295183" "ESB91415752" "ESB11013182" ...
 $ sctrs       : chr  "Construction" "Construction" "Construction" "Construction" ...
 $ Region   : chr  "Andalucia" "Andalucia" "Andalucia" "Andalucia" ...
 $ status              : num  1 1 1 1 1 1 1 1 1 1 ...
 $ EBIT.TA             : num  -0.191241 -0.000178 -0.043836 0.11546 -0.013383 ...
 $ EBITDA.TA           : num  -0.174524 -0.000122 -0.043808 0.119511 -0.013383 ...
 $ NI.SALES            : num  -1.0166 NA -0.0394 0.0624 NA ...
 $ EQ.TA               : num  0.32971 -0.00611 -0.01793 0.36638 -0.01101 ...
 $ CL.TA               : num  0.33 0.128 0.952 0.441 0.913 ...
 $ logSALES            : num  12.6 NA 16.3 13.6 NA ...
 $ logTA               : num  14.5 14.8 15.5 13.7 14.6 ...
 $ WC.TA               : num  0.524 0.621 0.796 0.698 0.893 ...
 $ SALES.TA            : num  0.152 NA 2.261 0.961 NA ...
 $ LTD.TA              : num  0.3399 0.8783 0.0659 0.1928 0.0982 ...
 $ WC.SALES            : num  3.454 NA 0.352 0.726 NA ...
 $ EBIT.CAP            : num  -2.99627 -0.00233 -75.28144 3.35609 -0.06732 ...
 $ Major.sectors.id    : num  3 3 3 3 3 3 3 3 3 3 ...
 $ Region.in.country.id: num  11 11 11 11 11 11 11 11 11 11 ...

该列表名为split,因此访问第一个元素将是list[[1]]我想删除,并访问第二个元素`list [[2]],我想保留

如何使用list[[i]]观察结果删除0的元素?

可重复数据(保存为splitxgb

structure(list(Construction.Cataluna = structure(list(BvD.ID.number = "ESB60520251", 
    Major.sectors = "Construction", Region.in.country = "Cataluna", 
    status = 1, EBIT.TA = -0.00446559204081373, EBITDA.TA = 0.0101143751351764, 
    NI.SALES = NA_real_, EQ.TA = -0.123382654164705, CL.TA = 1.12338265416471, 
    logSALES = NA_real_, logTA = 11.2721397009827, WC.TA = 0.728896578923932, 
    SALES.TA = NA_real_, LTD.TA = NA_real_, WC.SALES = NA_real_, 
    EBIT.CAP = -0.0584026622296173, Major.sectors.id = 3, Region.in.country.id = 2), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = 3L, class = "data.frame"), `Food, beverages, tobacco.Cataluna` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Hotels & restaurants.Cataluna` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Machinery, equipment, furniture, recycling.Cataluna` = structure(list(
    BvD.ID.number = "ESA17015389", Major.sectors = "Machinery, equipment, furniture, recycling", 
    Region.in.country = "Cataluna", status = 1, EBIT.TA = -0.884337466274593, 
    EBITDA.TA = -0.848029829933004, NI.SALES = -0.853539855963781, 
    EQ.TA = -0.552135083517749, CL.TA = 1.4997884014915, logSALES = 13.6946595709542, 
    logTA = 13.6227446483084, WC.TA = 0.526768727074302, SALES.TA = 1.07456391911965, 
    LTD.TA = 0.0523466820262528, WC.SALES = 0.490216280019771, 
    EBIT.CAP = -10.112047808544, Major.sectors.id = 2, Region.in.country.id = 2), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = 2L, class = "data.frame"), `Other services.Cataluna` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Publishing, printing.Cataluna` = structure(list(
    BvD.ID.number = "ESB61181335", Major.sectors = "Publishing, printing", 
    Region.in.country = "Cataluna", status = 1, EBIT.TA = 0.11109107677028, 
    EBITDA.TA = 0.163983707154427, NI.SALES = 0.00866939456284166, 
    EQ.TA = 0.0492834931482554, CL.TA = 0.795532473367464, logSALES = 13.3919583601834, 
    logTA = 12.7317120021392, WC.TA = 0.125702023008619, SALES.TA = 1.93526904477577, 
    LTD.TA = 0.155184033484281, WC.SALES = 0.0649532546122981, 
    EBIT.CAP = 1.56335274542429, Major.sectors.id = 4, Region.in.country.id = 2), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = 4L, class = "data.frame"), `Construction.Comunidad Valenciana` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Food, beverages, tobacco.Comunidad Valenciana` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Hotels & restaurants.Comunidad Valenciana` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Machinery, equipment, furniture, recycling.Comunidad Valenciana` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Other services.Comunidad Valenciana` = structure(list(
    BvD.ID.number = "ESB12225926", Major.sectors = "Other services", 
    Region.in.country = "Comunidad Valenciana", status = 1, EBIT.TA = -0.137203773525798, 
    EBITDA.TA = -0.136206169900857, NI.SALES = -43.5061163451473, 
    EQ.TA = 0.475243125304193, CL.TA = 0.159472766646537, logSALES = 10.4173585344881, 
    logTA = 16.3268584531943, WC.TA = 0.00637412879101754, SALES.TA = 0.00271354354050435, 
    LTD.TA = 0.364996968314073, WC.SALES = 2.34900553312397, 
    EBIT.CAP = -1.87696308048616, Major.sectors.id = 5, Region.in.country.id = 3), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = 5L, class = "data.frame"), `Publishing, printing.Comunidad Valenciana` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), Construction.Madrid = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Food, beverages, tobacco.Madrid` = structure(list(
    BvD.ID.number = "ESA28733194", Major.sectors = "Food, beverages, tobacco", 
    Region.in.country = "Madrid", status = 1, EBIT.TA = -0.234432635519391, 
    EBITDA.TA = -0.129822136145576, NI.SALES = -0.0837473615846728, 
    EQ.TA = -0.0677579847115102, CL.TA = 1.0677511411619, logSALES = 12.9741649477364, 
    logTA = 11.8922040117688, WC.TA = -0.0786529156943123, SALES.TA = 2.95045954435647, 
    LTD.TA = NA_real_, WC.SALES = -0.0266578526198594, EBIT.CAP = -0.299985988510579, 
    Major.sectors.id = 1, Region.in.country.id = 1), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = 1L, class = "data.frame"), `Hotels & restaurants.Madrid` = structure(list(
    BvD.ID.number = "ESB85504959", Major.sectors = "Hotels & restaurants", 
    Region.in.country = "Madrid", status = 1, EBIT.TA = -0.582114677880617, 
    EBITDA.TA = -0.571450821329039, NI.SALES = -0.28017544559193, 
    EQ.TA = 0.0478282913638668, CL.TA = 0.323819370573986, logSALES = 13.3486619464118, 
    logTA = 12.617410475401, WC.TA = 0.016396756437914, SALES.TA = 2.07767913655237, 
    LTD.TA = 0.628299317022736, WC.SALES = 0.00789186171697436, 
    EBIT.CAP = -0.931899204244032, Major.sectors.id = 6, Region.in.country.id = 1), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = 6L, class = "data.frame"), `Machinery, equipment, furniture, recycling.Madrid` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Other services.Madrid` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame"), `Publishing, printing.Madrid` = structure(list(
    BvD.ID.number = character(0), Major.sectors = character(0), 
    Region.in.country = character(0), status = numeric(0), EBIT.TA = numeric(0), 
    EBITDA.TA = numeric(0), NI.SALES = numeric(0), EQ.TA = numeric(0), 
    CL.TA = numeric(0), logSALES = numeric(0), logTA = numeric(0), 
    WC.TA = numeric(0), SALES.TA = numeric(0), LTD.TA = numeric(0), 
    WC.SALES = numeric(0), EBIT.CAP = numeric(0), Major.sectors.id = numeric(0), 
    Region.in.country.id = numeric(0)), .Names = c("BvD.ID.number", 
"Major.sectors", "Region.in.country", "status", "EBIT.TA", "EBITDA.TA", 
"NI.SALES", "EQ.TA", "CL.TA", "logSALES", "logTA", "WC.TA", "SALES.TA", 
"LTD.TA", "WC.SALES", "EBIT.CAP", "Major.sectors.id", "Region.in.country.id"
), row.names = integer(0), class = "data.frame")), .Names = c("Construction.Cataluna", 
"Food, beverages, tobacco.Cataluna", "Hotels & restaurants.Cataluna", 
"Machinery, equipment, furniture, recycling.Cataluna", "Other services.Cataluna", 
"Publishing, printing.Cataluna", "Construction.Comunidad Valenciana", 
"Food, beverages, tobacco.Comunidad Valenciana", "Hotels & restaurants.Comunidad Valenciana", 
"Machinery, equipment, furniture, recycling.Comunidad Valenciana", 
"Other services.Comunidad Valenciana", "Publishing, printing.Comunidad Valenciana", 
"Construction.Madrid", "Food, beverages, tobacco.Madrid", "Hotels & restaurants.Madrid", 
"Machinery, equipment, furniture, recycling.Madrid", "Other services.Madrid", 
"Publishing, printing.Madrid"))

2 个答案:

答案 0 :(得分:3)

假设您的所有列表元素都是数据框(或矩阵,或与nrow一起使用的其他对象),这应该有效:

your_list[sapply(your_list, nrow) > 0]

答案 1 :(得分:1)

如果您要删除仅包含"" (blank values)的列表项,则可以将其设为:

split  <- list(a = 1:20, b = a:d, c = c("A", "B"), d = "")

然后

split [split != ""]

将从列表中删除项d