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>
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.
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"
When what I'd like is just:
Aanestad
Ackerman
Alarcon
...
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 ...
Thanks!
You were very, very close in your initial solution. But as Aniko remarked, you not to remove
NA
values before you can use unique.An example where we first create a similar
data.frame
and then useapply()
as you did -- but with an additional anonymous function that is used to combinena.omit()
andunique()
:unique
does not appear to have anna.rm
argument, but you can remove the missing values yourself before calling it:gives