【问题标题】:R data: removing rows with NA did not delete the rows [duplicate]R数据:使用NA删除行并没有删除行[重复]
【发布时间】:2017-06-22 21:29:16
【问题描述】:

我正在尝试删除第 1 列(下例中的第 4 行)中带有 NA 的行,但不是删除该行,而是将整个第 4 行替换为 NA:

dat <- data.frame(x1 = c(1,2,3, NA, 5), x2 = c(100, NA, 300, 400, 500))
> dat
#  x1  x2
#  1  1 100
#  2  2  NA
#  3  3 300
#  4 NA 400
#  5  5 500

 ad<- dat[dat$x1!=1,]
 > ad
#     x1  x2
#  2   2  NA
#  3   3 300
#  NA NA  NA
#  5   5 500

【问题讨论】:

  • df %&gt;% tidyr::drop_na(x1)
  • 1==NA 返回NA 并通过NA 进行索引给出NA - 例如:c(1)[NA]
  • 试试这个dat[is.na(dat$x1),]=NA

标签: r


【解决方案1】:

试试complete.cases:

ad <- dat[complete.cases(dat$x1), ]

只保留 dat$x1 不包含 NA 的行

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 2011-09-22
    • 2017-08-11
    • 1970-01-01
    • 2016-10-17
    • 2015-05-06
    • 2018-10-21
    • 2014-02-27
    • 2021-12-30
    相关资源
    最近更新 更多