[英]R using ignoring NA's when using unique
I am attempting to find/discard rows based on their similarity in column values and have the following example code: 我试图根据列值的相似性查找/舍弃行,并使用以下示例代码:
vec1 <- c("B","D","E","NA")
vec2 <- c("B","D","E","NA")
vec3 <- c("B","C","E","NA")
vec4 <- c("B","D","E","NA")
vec5 <- c("B","NA","E","E")
vec6 <- c("B","NA","NA","NA")
mat1 <- cbind(vec1,vec2,vec3,vec4,vec5,vec6)
mat1
vec1 vec2 vec3 vec4 vec5 vec6
[1,] "B" "B" "B" "B" "B" "B"
[2,] "D" "D" "C" "D" "NA" "NA"
[3,] "E" "E" "E" "E" "E" "NA"
[4,] "NA" "NA" "NA" "NA" "E" "NA"
rows = apply(mat1, 1, function(i) length(unique(i)) > 1 )
mat2 <- mat1[rows, ]
vec1 vec2 vec3 vec4 vec5 vec6
[1,] "D" "D" "C" "D" "NA" "NA"
[2,] "E" "E" "E" "E" "E" "NA
[3,] "NA" "NA" "NA" "NA" "E" "NA"
How may I change the code above to achieve this? 如何更改上面的代码以实现此目的? In the help file for
unique
it suggests there is an incomparables
argument, is this implemented and can it be used? 在
unique
帮助文件中,它表明存在一个incomparables
参数,该参数是否已实现并且可以使用? I don't necessarily wish to remove the NA
's just ignore them. 我不一定希望删除
NA
只是忽略它们。
从上面的评论
rows = apply(mat1, 1, function(i) length(unique(i[!(i=="NA")]))>1)
You'll need them to be properly entered for is.na
to work as it should. 您需要正确输入它们,
is.na
才能正常工作。 Currently they are character strings, and NA
has is classed to we can assign NA_character_
to be safe. 目前,它们是字符串,并且
NA
已经分类为我们可以分配NA_character_
为安全。 Also, I would recommend a summary
Here are a few table that get the information you need. 另外,我将建议
summary
这里有一些表格,可获取您所需的信息。
length(unique(...))
is essentially a table
length(unique(...))
本质上是一个table
> mat1[is.na(mat1)]
# character(0) ## not good
> mat1[mat1 == "NA"] <- NA_character_
> mat1[is.na(mat1)] ## notice the difference...
# [1] NA NA NA NA NA NA NA NA
> summary(mat1)
vec1 vec2 vec3 vec4 vec5 vec6
B :1 B :1 B :1 B :1 B :1 B :1
D :1 D :1 C :1 D :1 E :2 NA's:3
E :1 E :1 E :1 E :1 NA's:1
NA's:1 NA's:1 NA's:1 NA's:1
> apply(mat1, 2, function(x) length(table(x)))
vec1 vec2 vec3 vec4 vec5 vec6
3 3 3 3 2 1
> as.table(t(mat1))
A B C D
vec1 B D E
vec2 B D E
vec3 B C E
vec4 B D E
vec5 B E E
vec6 B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.