Any help on this would be much appreciated.
对此任何帮助都将不胜感激。
I have A and B and want to get AB as the max of A or B and account for NAs
我有A和B,想把AB作为A或B的最大值,并考虑到NAs。
A<-c(1,1,1,0,0,0,NA,NA,NA)
B<-c(1,0,NA,1,0,NA,1,0,NA)
AB<-c(1,1,1,1,0,0,1,0,NA)
TEST<-cbind(A,B,AB)
A B AB
[1,] 1 1 1
[2,] 1 0 1
[3,] 1 NA 1
[4,] 0 1 1
[5,] 0 0 0
[6,] 0 NA 0
[7,] NA 1 1
[8,] NA 0 0
[9,] NA NA NA
1 个解决方案
#1
4
Use the pmax()
function with na.rm=TRUE
:
使用带na.rm=TRUE的pmax()函数:
> AB <- pmax(A, B, na.rm=TRUE)
> TEST <- cbind(A, B, AB)
> TEST
A B AB
[1,] 1 1 1
[2,] 1 0 1
[3,] 1 NA 1
[4,] 0 1 1
[5,] 0 0 0
[6,] 0 NA 0
[7,] NA 1 1
[8,] NA 0 0
[9,] NA NA NA
#1
4
Use the pmax()
function with na.rm=TRUE
:
使用带na.rm=TRUE的pmax()函数:
> AB <- pmax(A, B, na.rm=TRUE)
> TEST <- cbind(A, B, AB)
> TEST
A B AB
[1,] 1 1 1
[2,] 1 0 1
[3,] 1 NA 1
[4,] 0 1 1
[5,] 0 0 0
[6,] 0 NA 0
[7,] NA 1 1
[8,] NA 0 0
[9,] NA NA NA