处理适用和唯一的NA值 [英] Handling NA values in apply and unique

查看:85
本文介绍了处理适用和唯一的NA值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个114行16列的数据帧,其中的行是独立的,各列是它们的名称或不适用.例如,前3行如下所示:

I have a 114 row by 16 column data frame where the rows are individuals, and the columns are either their names or NA. For example, the first 3 rows looks like this:

            name name.1      name.2 name.3       name.4 name.5       name.6 name.7       name.8 name.9       name.10 name.11       name.12 name.13        name.14 name.15
1           <NA>   <NA>        <NA>   <NA>         <NA>   <NA>         <NA>   <NA>         <NA>   <NA>      Aanestad    <NA>      Aanestad    <NA>       Aanestad    <NA>
2           <NA>   <NA>        <NA>   <NA>         <NA>   <NA>         <NA>   <NA>     Ackerman   <NA>      Ackerman    <NA>      Ackerman    <NA>       Ackerman    <NA>
3           <NA>   <NA>        <NA>   <NA>         <NA>   <NA>      Alarcon   <NA>      Alarcon   <NA>       Alarcon    <NA>       Alarcon    <NA>           <NA>    <NA>

我想为所有唯一名称生成一个列表(如果每行有多个唯一名称)或向量(如果每行只有一个唯一名称),长度为114.

I want to generate a list (if multiple unique names per row) or vector (if only one unique name per row) of all the unique names, with length 114.

当我尝试apply(x,1,unique)时,我得到一个2xNcol数组,其中有时第一行单元格为NA,有时第二行单元格为NA.

When I try apply(x,1,unique) I get a 2xNcol array where sometimes the first row cell is NA and sometimes the second row cell is NA.

    [,1]       [,2]       [,3]      [,4]     [,5]      [,6]      [,7]    [,8]   [,9]    
[1,] NA         NA         NA        NA       "Alquist" NA        "Ayala" NA     NA      
[2,] "Aanestad" "Ackerman" "Alarcon" "Alpert" NA        "Ashburn" NA      "Baca" "Battin"

当我想要的只是:

Aanestad
Ackerman
Alarcon
...

在忽略NA的同时,我似乎无法弄清楚如何应用unique(). na.rm,na.omit等似乎不起作用.我觉得我想念一些真正简单的东西...

I can't seem to figure out how to apply unique() while ignoring NA. na.rm, na.omit etc don't seem to work. I feel like I'm missing something real simple ...

谢谢!

推荐答案

unique似乎没有na.rm参数,但是您可以在调用它之前自己删除缺失的值:

unique does not appear to have an na.rm argument, but you can remove the missing values yourself before calling it:

A <- matrix(c(NA,"A","A",
             "B", NA, NA,
              NA, NA, "C"), nr=3, byrow=TRUE)
apply(A, 1, function(x)unique(x[!is.na(x)]))

给予

[1] "A" "B" "C"

这篇关于处理适用和唯一的NA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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