r中的和NA值

时间:2015-06-15 15:36:56

标签: r sorting na

我正在使用具有多个NA值的数据帧,因此我考虑根据其NA值对属性进行排序。 我试图使用for循环,这是我到目前为止:

> data <- read.csv("C:/Users/Nikita/Desktop/first1k.csv")
> for (i in 1:length(data) ) {
+ temp <- c(sum(is.na(data[i])))}
> temp
[1] 0

这是我第一次在r中使用for循环,所以我确信它只是一个愚蠢的语法问题,但我无法理解哪一个。

最终,我需要一个列表,显示属性的名称及其NA计数。这样我就可以对列表进行排序并获得所需的信息。这是一些模拟数据,使其更容易。

data <- data.frame(A = c(500, 600, 700, 1000),
                   B = c(500, 600, 700, NA),
                   C = c(NA, NA, 500, 700),
                   D = c(800, NA, 933, NA),
                   E = c(NA, NA, NA, NA))

编辑: 谢谢大家的帮助。这三种解决方案都适合我。我确实想知道是否有一行代码在将它们导出到文件之前对这些属性进行排序。就像我之前提到的那样,我在r中很新,所以我不确定它是否可能。

编辑2: 当我运行sort时,会给我下一个错误:

temp <- sort(temp)
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic

知道为什么吗?

3 个答案:

答案 0 :(得分:7)

以下是使用Error: Invalid value for <path> attribute d="MNaN,135.1004464285714LNaN,137.4441964285714CNaN,139.7879464285714,NaN,144.47544642857142,NaN,147.48883928571428CNaN,150.50223214285714,NaN,151.84151785714283,NaN,152.76227678571428CNaN,153.6830357142857,NaN,154.18526785714283,NaN,154.52008928571428CNaN,154.85491071428572,NaN,155.02232142857142,NaN,156.52901785714283CNaN,158.03571428571428,NaN,160.8816964285714,NaN,162.55580357142856CNaN,164.22991071428567,NaN,164.7321428571428,NaN,164.89955357142853CNaN,165.06696428571425,NaN,164.89955357142856,NaN,164.89955357142856CNaN,164.89955357142856,NaN,165.06696428571425,NaN,162.30468749999997CNaN,159.54241071428567,NaN,153.8504464285714,NaN,150.1674107142857CNaN,146.48437499999997,NaN,144.81026785714283,NaN,143.63839285714283CNaN,142.46651785714283,NaN,141.79687499999997,NaN,139.11830357142856CNaN,136.4397321428571,NaN,131.7522321428571,NaN,127.06473214285714CNaN,122.37723214285712,NaN,117.68973214285712,NaN,117.43861607142857CNaN,117.1875,NaN,121.37276785714286,NaN,125.97656249999999CNaN,130.58035714285714,NaN,135.60267857142853,NaN,138.53236607142853CNaN,141.46205357142853,NaN,142.2991071428571,NaN,144.30803571428567CNaN,146.31696428571425,NaN,149.4977678571428,NaN,151.33928571428567CNaN,153.18080357142853,NaN,153.68303571428567,NaN,155.35714285714283CNaN,157.03124999999997,NaN,159.8772321428571,NaN,161.96986607142856CNaN,164.06249999999997,NaN,165.40178571428572,NaN,162.47209821428572CNaN,159.54241071428572,NaN,152.34374999999997,NaN,145.0613839285714CNaN,137.77901785714283,NaN,130.41294642857142,NaN,130.24553571428572CNaN,130.078125,NaN,137.109375,NaN,144.64285714285714CNaN,152.17633928571428,NaN,160.21205357142856,NaN,165.48549107142856CNaN,170.75892857142856,NaN,173.27008928571428,NaN,173.27008928571428CNa.....(not these numbers don't match the numbers in the tsv file, so I don't know where they're coming from) Error: Invalid value for <path> attribute d="MNaN,450LNaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,4(ditto don't know where these numbers are coming from Error: Invalid value for <path> attribute d="MNaN,450LNaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,450CNaN,450,NaN,450,NaN,4...ditto Error: Invalid value for <text> attribute transform="translate(NaN,173.27008928571425)"o @ d3.min.js:1(anonymous function) @ d3.min.js:3Y @ d3.min.js:1_a.each @ d3.min.js:3_a.attr @ d3.min.js:3(anonymous function) @ index2.html:121(anonymous function) @ d3.min.js:1t @ d3.min.js:1u @ d3.min.js:1 d3.min.js:1 Error: Invalid value for <text> attribute transform="translate(NaN,450)"o @ d3.min.js:1(anonymous function) @ d3.min.js:3Y @ d3.min.js:1_a.each @ d3.min.js:3_a.attr @ d3.min.js:3(anonymous function) @ index2.html:121(anonymous function) @ d3.min.js:1t @ d3.min.js:1u @ d3.min.js:1 d3.min.js:1 Error: Invalid value for <text> attribute transform="translate(NaN,88.89508928571426)" is.na的快速回答:

colSums

返回:

colSums(is.na(data))

以上数据。

感谢@akrun显示我的剩余 A B C D E 0 1 2 2 4

答案 1 :(得分:3)

在R中执行迭代代码的正确方法是避免显式for循环。请改用apply(和公司)。 @jeremycg给了你正确的R-ish答案。关于你的代码,你应该做一些编辑才能使它工作。

temp <- c()
for (i in 1:length(data)){
    temp[names(data)[i]] <- sum(is.na(data[i]))
}

您在每次迭代时都重写了temp。此外,您没有将变量的标签写入temp。因此,您看到的输出是数据集最后一列中NA的数量。

关于OP的编辑

temp <- sort(temp) # pass decreasing=T into arguments in case
                   # you want reversed order

答案 2 :(得分:1)

这个答案显示了如何使for循环工作。

temp <- vector(length = ncol(data))

for (i in 1:length(data)) {
   temp[i] <- c(sum(is.na(data[, i])))
}

names(temp) <- colnames(data)

temp
# A B C D E 
# 0 1 2 2 4