在 apply 和 unique 中处理 NA 值 [英] Handling NA values in apply and unique

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

问题描述

我有一个 114 行 x 16 列的数据框,其中行是个人,列是他们的名字或 NA.例如,前 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"

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

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