r - 在 R 的数据框中选择 NA obs
问题描述
说,这是我的数据
data=structure(list(x1 = structure(c(1L, 7L, 2L, 8L, 4L, 5L, 11L,
9L, 3L, 6L, 10L), .Label = c("1270", "14130", "2030", "29910",
"310", "3160", "570", "620", "7520", "960", "na"), class = "factor"),
x2 = structure(c(6L, 2L, 7L, 6L, 4L, 3L, 4L, 1L, 5L, 6L,
2L), .Label = c("10", "11", "12", "4", "8", "9", "na"), class = "factor"),
x3 = structure(c(4L, 3L, 2L, 5L, 9L, 7L, 7L, 8L, 1L, 5L,
6L), .Label = c("2000", "2006", "2007", "2008", "2009", "2011",
"2013", "2014", "na"), class = "factor"), Date = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "26.11.2014", class = "factor"),
Sales = c(5577L, 5919L, 6911L, 13307L, 5640L, 6555L, 11430L,
6401L, 8072L, 6350L, 10031L), id = 1:11), .Names = c("x1",
"x2", "x3", "Date", "Sales", "id"), class = "data.frame", row.names = c(NA,
-11L))
我需要通过观察 id+Date 来获取数据帧,其中至少有一个 NA(缺失值)在此示例输出中将是
Date id
26.11.2014 3
26.11.2014 5
26.11.2014 7
怎么做?
解决方案
您没有缺失值而是"na"
字符串,因此我们首先将它们转换:
data[data=="na"] <- NA
data[!complete.cases(data),]
# x1 x2 x3 Date Sales id
# 3 14130 <NA> 2006 26.11.2014 6911 3
# 5 29910 4 <NA> 26.11.2014 5640 5
# 7 <NA> 4 2013 26.11.2014 11430 7
为了让您"na"
的价值观保持原样,请执行以下操作:
data[rowSums(data =="na") >0,]
# x1 x2 x3 Date Sales id
# 3 14130 na 2006 26.11.2014 6911 3
# 5 29910 4 na 26.11.2014 5640 5
# 7 na 4 2013 26.11.2014 11430 7
推荐阅读
- python - Python3将2列表与字典元素进行比较
- android - 使用 Gradle 任务运行单元测试时出现 NoClassDefFoundError
- php - 如何使javascript图形只有唯一值
- iis - 在 IIS (Ip) 上发布的应用程序不知道我的 node_modules
- java - 如何从数组中的文件路径中获取字节数组?
- android - 警报管理器不适用于 Android 版本 9
- ruby-on-rails - Rails 中多线程单例对象的策略
- java - 在不使用 java 提取 zip 文件的情况下更新 Zip 文件或 zip 文件中的文件
- javascript - 如何使用 jest 测试反应组件?
- python - 在 python 文件中搜索所有 if 条件并在下一行添加 print 语句