每个NA值只显示一行 [英] Display only one line for each NA Value

查看:155
本文介绍了每个NA值只显示一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本的某个时刻,我喜欢在我的 data.frame 缺少值的数量
$ c>并显示它们。
在我的情况下,我有:

At some point in my script I like to see the number of missing values in my data.frame and display them. In my case I have:

out <- read.csv(file="...../OUT.csv", na.strings="NULL")

sum(is.na(out$codeHelper))

out[is.na(out$codeHelper),c(1,length(colnames(out)))]

它工作得很好。
然而,最后一个命令显然给了我整个 data.frame 其中 NA TRUE ,例如:

It works perfectly fine. However, the last command obviously gives me the whole data.frame where the NA is TRUE, eg:

5561                  Yemen (PDR) <NA>
5562                  Yemen (PDR) <NA>
5563                  Yemen (PDR) <NA>
5564                  Yemen (PDR) <NA>
5565                  Yemen (PDR) <NA>
5566                  Yemen (PDR) <NA>
5567                  Yemen (PDR) <NA>
5568                  Yemen (PDR) <NA>
5601 Zaire (Democ Republic Congo) <NA>
5602 Zaire (Democ Republic Congo) <NA>
5603 Zaire (Democ Republic Congo) <NA>
5604 Zaire (Democ Republic Congo) <NA>
5605 Zaire (Democ Republic Congo) <NA>

有一个大框架和很多NAs看起来很混乱。
对我来说重要的只是NA发生的地方,哪个国家
(在第二列)在第三列中缺少值。

With a big frame and a lot of NAs that looks pretty messy. Important to me is only where the NA occurs i.e which country (in the second column) has a missing value in the third column.

那么我怎么只能为每个国家显示一行?

So how can i only display a single row for each country?

它应该是这样的:

    1                  Yemen (PDR) <NA>
    2 Zaire (Democ Republic Congo) <NA>
    3                          USA <NA>
    4                     W. Samoa <NA>


推荐答案

unique(c(1 ,2,3,4,4))

将给你


1 2 3 4

1 2 3 4

so

unique(out [is.na(out $ codeHelper),c(1,length(colnames(out)))])

应该是你要找的?

这篇关于每个NA值只显示一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆