Handling NA values in apply and unique

前端 未结 2 1950
小蘑菇
小蘑菇 2021-01-04 06:20

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:

2条回答
  •  太阳男子
    2021-01-04 07:15

    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)]))
    

    gives

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

提交回复
热议问题