删除所有列中带有“ NA”的行

时间:2019-08-16 06:24:55

标签: r dataframe

您好,我的数据框由在所有列中均具有NA的行组成,我想将其删除。

这是我的数据框示例的样子:

Date       Blk 3    Blk 3    Blk3     Total
           Lvl 2-25 Lvl 2-26 Lvl 2-27 
2019-01-02  1       20       10        31
2019-01-02  NA      NA       NA        NA
2019-01-03  NA      10       30        40

,我想只删除第二行。并在删除后如下所示:

Date       Blk 3    Blk 3    Blk3     Total
           Lvl 2-25 Lvl 2-26 Lvl 2-27 
2019-01-02  1       20       10        31
2019-01-03  NA      10       30        40

我已经尝试过这样的方法:

df <- df %>% remove_empty("rows") #using janitor package

df <- df %>% filter_all(any_vars(!is.na(.)))
df <- df %>% filter_all(any_vars(complete.cases(.))) 

df <- df[rowSums(is.na(df)) != ncol(df), ]

但是它们都不起作用。有人可以帮我吗?

这是我的dput(df):

structure(list(Date = structure(c(17777, 17778, 17779, 17780, 
17781, 17782), class = "Date"), `Blk 3 Lvl 2-25` = c(300, 200, 
600, 600, 250, 800), `Blk 3 Lvl 2-26` = c(400, 120, 400, 3000, 
150, 1200), `Blk 3 Lvl 2-27` = c(500, 90, 120, 300, 800, 200), 
    `Blk 3 Lvl 2-28` = c(80, 800, 150, 500, 1500, 800), `Blk 3 Lvl 2-29` = c(50, 
    300, 1.2, 80, 120, 12), `Blk 3 Lvl 2-30` = c(150, 30, 60, 
    300, 1500, NA), `Blk 3 Lvl 2-31` = c(30, 600, 400, 2000, 
    250, NA), `Blk 3 Lvl 2-32` = c(2000, 3000, 600, 1200, 900, 
    NA), `Blk 3 Lvl 2-33` = c(250, 200, 200, 150, 250, NA), `Blk 3 Lvl 2-34` = c(1500, 
    5000, 1.2, 3000, 600, NA), `Blk 3 Lvl 2-35` = c(2000, 1500, 
    1500, 3000, 1500, NA), `Blk 3 Lvl 2-36` = c(1500, 1500, 1500, 
    2000, 1500, NA), `Blk 3 Lvl 2-37` = c(400, 800, 3000, 600, 
    1200, NA), `Blk 3 Lvl 2-38` = c(600, 1200, 1200, 2000, 800, 
    NA), `Blk 3 Lvl 5-25` = c(2000, 1200, 900, 3000, 900, NA), 
    `Blk 3 Lvl 5-26` = c(200, 200, 1.2, 400, 600, NA), `Blk 3 Lvl 5-27` = c(800, 
    800, 600, 800, 400, NA), `Blk 3 Lvl 5-28` = c(800, 800, 1.2, 
    800, 2000, NA), `Blk 3 Lvl 5-29` = c(1200, 1200, 800, 600, 
    900, NA), `Blk 3 Lvl 5-30` = c(1500, 1200, 900, 400, 1200, 
    NA), `Blk 3 Lvl 5-31` = c(400, 800, 1.2, 12, 90, NA), `Blk 3 Lvl 5-32` = c(150, 
    120, 200, 300, 150, NA), `Blk 3 Lvl 5-33` = c(500, 600, 900, 
    300, 900, NA), `Blk 3 Lvl 5-34` = c(300, 300, 1.2, 900, 1200, 
    NA), `Blk 3 Lvl 5-35` = c(200, 250, 300, 200, 200, NA), `Blk 3 Lvl 5-36` = c(900, 
    1200, 3000, 2000, 1500, NA), `Blk 3 Lvl 5-37` = c(800, 1500, 
    2000, 2000, 3000, NA), `Blk 3 Lvl 5-38` = c(600, 200, 3000, 
    80, 400, NA), `Blk 3 Lvl 7-25` = c(900, 400, 1200, 800, 400, 
    NA), `Blk 3 Lvl 7-26` = c(1500, 800, 3000, 800, 600, NA), 
    `Blk 3 Lvl 7-27` = c(800, 400, 300, 400, 400, NA), `Blk 3 Lvl 7-28` = c(200, 
    200, 200, 30, 12, NA), `Blk 3 Lvl 7-29` = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-30` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-31` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-32` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-33` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-34` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-35` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-36` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-37` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Blk 3 Lvl 7-38` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Total = c(23510, 
    27510, 27037.2, 32552, 26172, 3012)), row.names = c(NA, 6L
), class = "data.frame")

这是我实际数据框的外观,我想删除第7行之类的行: dataframe

2 个答案:

答案 0 :(得分:0)

问题是在计算new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='mi-title' and text()='Staging - ALCDE'][starts-with(@data-ng-bind, 'navverTreeItemScope')]"))).click(); 值时需要排除多于1列。我们可以仅选择所需的列并执行NA

rowSums

或者使用cols <- grep("^Blk", names(df)) df[rowSums(is.na(df[cols])) != length(cols), ]

dplyr

答案 1 :(得分:0)

relatedProducts中的一个选项是选择感兴趣的列('nm1'),使用base R遍历这些列,检查它们是否为NA以转换为逻辑向量{{1 }}转换为带有lapply的单个逻辑矢量,取反(Reduce)并对行进行子集

&

也可以使用!

nm1 <- grep("^Blk", names(df))
df[!Reduce(`&`, lapply(df[nm1], is.na)),]