将两个变量组合成新变量

时间:2015-11-17 02:34:27

标签: r if-statement

对此的任何帮助都将非常感激。

我有A和B,并希望将AB作为A或B的最大值并考虑到NA

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 个答案:

答案 0 :(得分:4)

public static int[] replicate(int[] arr, int n) { int[] temp = new int[arr.length]; int current=0; int count = 0; for (int i = 0; i < arr.length; i++) { if (count%n!=0){ temp[count] = arr[i]; count++; } } int[] whitelist = new int[count]; System.arraycopy(temp, 0, whitelist, 0, count); return whitelist; } 功能与pmax()

一起使用
na.rm=TRUE