每个NA值只显示一行

编程入门 行业动态 更新时间:2024-10-28 20:31:13
本文介绍了每个NA值只显示一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的脚本的某个时刻,我喜欢在我的 data.frame 并显示它们。 在我的情况下,我有:

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值只显示一行

本文发布于:2023-10-25 22:36:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:只显示   NA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!