【问题标题】:remove rows where all columns are NA except 2 columns [duplicate]删除除 2 列外所有列均为 NA 的行[重复]
【发布时间】:2016-02-10 00:44:15
【问题描述】:

我有一个data.table。我想删除除某些 2 列之外的所有列都是 NA 的那些行。例如:

我有一个 data.table 像:

> ww2
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species index
 1:          5.1         3.5          1.4         0.2  setosa     1
 2:          4.9         3.0          1.4         0.2  setosa     2
 3:          4.7         3.2          1.3         0.2  setosa     3
 4:          4.6         3.1          1.5         0.2  setosa     4
 5:          5.0         3.6          1.4         0.2  setosa     5
 6:          5.1         3.5          1.4         0.2 dffdsdf     1
 7:          4.9         3.0          1.4         0.2 dffdsdf     2
 8:          4.7         3.2          1.3         0.2 dffdsdf     3
 9:           NA          NA           NA          NA dffdsdf     4
10:           NA          NA           NA          NA dffdsdf     5

它的输出是:

    structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.1, 4.9, 
4.7, NA, NA), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.5, 3, 
3.2, NA, NA), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.4, 
1.4, 1.3, NA, NA), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 
0.2, 0.2, NA, NA), Species = structure(c(1L, 1L, 1L, 1L, 1L, 
4L, 4L, 4L, 4L, 4L), class = "factor", .Label = c("setosa", "versicolor", 
"virginica", "dffdsdf")), index = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L)), .Names = c("Sepal.Length", "Sepal.Width", "Petal.Length", 
"Petal.Width", "Species", "index"), row.names = c(NA, -10L), class = "data.frame")

在上面的数据表中,我想删除第 9 行和第 10 行。由于我的实际数据表非常大并且包含更多列,因此很难明确提及那些不适用的列。但是不是 NA 的列是固定的(它们是 2,在这个特定的示例中它们是 indexSpecies

我正在寻找一种高效且快速的解决方案。

【问题讨论】:

  • @Pascal 不是。在那里,想要删除任何列为 NA 的那些行(不管 1 列还是 2 列或 3 列是 NA)。但在这里,我想删除那些列数固定为 NA 的行。
  • dput 给出错误错误:“”中的意外'
  • 所以只需在.SDcols 中指定它们?例如ww2[!ww2[, Reduce('&', lapply(.SD, is.na)), .SDcols = -(Species:index)]]
  • @user3664020 我很确定副本中的答案可以修改。
  • @RonakShah 我已经编辑了dput,请将其转换为data.table。使用dt <- data.table(df)

标签: r data.table


【解决方案1】:

鉴于您提供的数据,我会这样做:

library(dplyr)
na_rows = ww2 %>% 
            select(-Species, -index) %>% 
            is.na() %>% 
            rowSums() > 0

ww2 %>% 
  filter(!na_rows)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species index
1          5.1         3.5          1.4         0.2  setosa     1
2          4.9         3.0          1.4         0.2  setosa     2
3          4.7         3.2          1.3         0.2  setosa     3
4          4.6         3.1          1.5         0.2  setosa     4
5          5.0         3.6          1.4         0.2  setosa     5
6          5.1         3.5          1.4         0.2 dffdsdf     1
7          4.9         3.0          1.4         0.2 dffdsdf     2
8          4.7         3.2          1.3         0.2 dffdsdf     3

或更多默认 R 样式(我喜欢 dplyr):

na_rows = rowSums(is.na(ww2[, .SD, .SDcols = !c('Species', 'index')]), with = FALSE])) > 0
ww2[!na_rows,]

【讨论】:

  • 好的,as.data.frame ;)?
猜你喜欢
  • 2019-11-14
  • 2018-10-21
  • 2022-01-16
  • 2020-07-22
  • 2014-11-13
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2021-05-28
相关资源
最近更新 更多